Rotate player script does not work?

So I have this script that when you equip the tool, the player starts following the mouse, but if you reset it does not work anymore. so I tried this script (I added the humanoid died function) and now it does not work at all, any help? There are no output errors either.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character.Humanoid
local Mouse = Player:GetMouse()
local Tool = script.Parent

local Equipped = false

Tool.Equipped:Connect(function()
	if Equipped == false then
		Equipped = true
		while Equipped do
			Character.Humanoid.AutoRotate = false
			local direction = (Mouse.Hit.p - Character.HumanoidRootPart.Position) * Vector3.new(1, 0, 1)
			Character.HumanoidRootPart.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Character.HumanoidRootPart.Position + direction)
			wait()
		end
	end
end)

Tool.Unequipped:Connect(function()
				Character.Humanoid.AutoRotate = true
	Equipped = false
end)

Humanoid.Died:connect(function()
				Character.Humanoid.AutoRotate = true
	Equipped = false	
end)

Every time the player’s character dies, a brand new character and humanoid is created and the old dead character is destroyed.
I think what you could do is connect to the player’s CharacterAdded event instead of the humanoid’s Died event, and redefine the Character and Humanoid variables there:

Player.CharacterAdded:Connect(function(character)
    Character = character
    Humanoid = character:WaitForChild("Humanoid")
end)

That way, your Tool’s Equipped and Unequipped events have updated variables to modify.

2 Likes

This uses LocalPlayer so this is not needed, I have made a script like this for someone OP so give me a second to find it.

Edit: Apparently tools do not reset like ScreenGui so yes you will have to update the old data, my apologies.

1 Like