#Character Rotation script breaks when replacing the player character

1 messages · Page 1 of 1 (latest)

orchid mica
#

When I try to morph the player character into another model everything goes fine except for my character rotation script, which breaks because "player.character is null". this is the code for the character swap:

            local oldChar = plr.Character
            local newChar = rs:WaitForChild("CharacterModels"):WaitForChild(arg1):Clone()
            
            newChar.Name = plr.Name

            newChar.HumanoidRootPart.Anchored = false
            newChar:SetPrimaryPartCFrame(oldChar.PrimaryPart.CFrame)
            
            oldChar:Destroy()
            plr.Character = newChar
            newChar.Parent = workspace

and this is the code for the rotation

local cam = workspace.CurrentCamera
local plr = game:GetService("Players").LocalPlayer

game:GetService("RunService").RenderStepped:Connect(function()
    local char = plr.Character
    if char then
        local rootPart = char:WaitForChild("HumanoidRootPart")
        if rootPart then
            rootPart.CFrame = CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + Vector3.new(cam.CFrame.LookVector.X,0,cam.CFrame.LookVector.Z))
        end
    end
end)
thorny grail
#

Maybe its an onCharacterAdded issue just search the term “onCharacterAdded” let me know if it helps or not, im new as well so im wondering also

orchid mica
#

tried that but for some reason CharacterAdded doesn't even trigger when I change my character model so I'm really not sure

orchid mica
#

I finally figured it out and it was incredibly dumb... I had accidently put it in starterCHARACTERscripts instead of starterplayerscripts