Tool LocalScript problem

Hello devs, I’m working on game and I created tool with LocalScript with it :

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local holdAnim = humanoid.Animator:LoadAnimation(script.Parent:WaitForChild("Hold"))
local toggleAnim = humanoid.Animator:LoadAnimation(script.Parent:WaitForChild("Toggle"))
local event = script.Parent:WaitForChild("Event")

local toggle = false
local debounce = false

script.Parent.Unequipped:Connect(function()
	holdAnim:Stop()
	toggleAnim:Stop()
	
end)

script.Parent.Equipped:Connect(function()
	if toggle == true then
		holdAnim:Play()
	end
end)

script.Parent.Activated:Connect(function()
	if toggle == false and debounce == false then
		debounce = true
		toggle = true
		script.Parent.Sound:FireServer()
		toggleAnim:Play()
		toggleAnim.Stopped:Wait()
		event:FireServer()
		holdAnim:Play()
		debounce = false
	elseif toggle == true and debounce == false then
		debounce = true
		toggle = false
		script.Parent.Sound:FireServer()
		holdAnim:Stop()
		toggleAnim:Play()
		toggleAnim.Stopped:Wait()
		event:FireServer()
		debounce = false
	end
end)

At first it works fine but after player dies this error shows up :

error

Hope you can help me! Thanks!

Where is this local script located (Explorer)

image_2021-05-13_144723
Its told in topic,

Tool is in the StarterPack yes?

The player’s character resets every time when you die and a new humanoid is formed.
Try to put the local script in game.StarterPlayer.StarterCharacterScripts

Yeah it is, need to extend this.

But tool wont work then… You need to think a bit.

No, it will work but you need to use game.StarterPack.Flashlight instead of script.Parent

Tool is moving to Character when equipped bruh.

1 Like

You can use too CharacterAdded it’s the same

Try defining the humanoid inside a function.

local holdAnim
local toggleAnim

player.CharacterAdded:Connect(function(character)
    local humanoid = character:WaitForChild("Humanoid")
    local Animator = humanoid:FindFirstChildOfClass("Animator")
    holdAnim = Animator:LoadAnimation(script.Parent:WaitForChild("Hold"))
    toggleAnim = Animator:LoadAnimation(script.Parent:WaitForChild("Toggle"))
end)

Fixed it by getting all children and descendats when tool is equipped and enabling all of gotten scripts! So now it works fine :smiley: .

Thank you for help everyone!

2 Likes