Script doesn't work when player dies

I have a script that works fine but when I reset my character, it doesn’t work anymore
I need help!

[This problem appears in all my scripts]

local humrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")

local part = workspace.Part

while true do
	humrp.CFrame = CFrame.lookAt(humrp.Position, Vector3.new(part.Position.X, humrp.Position.Y, part.Position.Z))
	task.wait()

end
2 Likes

Where is your script located?
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

1 Like

This is happening due everytime the player die, the character is changed.
Basically you need to update the humrp & hum variables everytime the player character be spawned.

1 Like

It works just fine if you put it in starter character scripts

Capture

or you can try this

local part = workspace.Part

while true do
	local humrp = char:WaitForChild("HumanoidRootPart")
	local hum = char:WaitForChild("Humanoid")
	
	humrp.CFrame = CFrame.lookAt(humrp.Position, Vector3.new(part.Position.X, humrp.Position.Y, part.Position.Z))
	task.wait()
end
3 Likes

Sorry, but it doesn’t work.

btw i put the localscripts on starterplayerscripts

1 Like

How are you getting the character?

1 Like

yeah, i know that but i don’t know how to do that

1 Like

1 Like

Try

local Player = game:GetService("Players").LocalPlayer
local part = workspace.Part

while true do
	local char = Player.Character or Player.CharacterAdded:Wait()
	local humrp = char:WaitForChild("HumanoidRootPart")
	local hum = char:WaitForChild("Humanoid")

	humrp.CFrame = CFrame.lookAt(humrp.Position, Vector3.new(part.Position.X, humrp.Position.Y, part.Position.Z))
	task.wait()
end

It works! I need to learn more about loops sorry ^^

It would have worked if you moved the script to the ‘StarterCharacterScripts’ container.

1 Like