For some reason this stops working when I reset?

Hi, So basically, I’m trying to make the player’s character face in the direction of the mouse… But…

Once I die/reset, it stops working, but the event still fires…? How???

local runservice = game:GetService("RunService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Root = character.PrimaryPart

local mouse = player:GetMouse()
local event = game:GetService("ReplicatedStorage"):WaitForChild("BodyToMouseCFrameEvent")

runservice.RenderStepped:Connect(function()
	event:FireServer(math.asin(mouse.Hit.LookVector.Y),character.Humanoid.RigType)
	if character:FindFirstChild("HumanoidRootPart") then
		character:SetPrimaryPartCFrame(CFrame.new(character.HumanoidRootPart.CFrame.p, character.HumanoidRootPart.CFrame.p + Vector3.new(mouse.Hit.LookVector.X * 100, 0, mouse.Hit.LookVector.Z * 100)))
	end
end)

(its a local script if you couldn’t tell!)

Oh, and it DEFINITELY has the humanoidrootpart, because when I hooked up an error message to the else from the if character:FindFirstChild("HumanoidRootPart") then, the error didn’t fire.

1 Like

You never reassigned the character variable to the new character that the player respawned with, so it’s always using the old one. As to why it didn’t error, that’s because the old character still exists within memory (it’s not actually deleted due to it still having an active reference) and you’re just using that old one

2 Likes

Like @Prototrode said, either reassign character e̶v̶e̶r̶y̶ f̶r̶a̶m̶e̶ or on death, or put the script in starter character scripts.

1 Like

Or just use a connection to update it

local character = player.Character or player.CharacterAdded:Wait()
local Root = character.PrimaryPart
player.CharacterAdded:Connect(function()
  character = player.Character
  Root = character.PrimaryPart
end)
3 Likes

The ‘StarterCharacterScripts’ container is an unnecessary evil.

1 Like