Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Animation script stop working when I die/respawn?

I have this tool that if I shift sprint with it, it plays this custom animation I have used, but when I reset, I get this error on Line 22 saying "LoadAnimation requires the Humanoid object (TechModel.Humanoid) to be a descendant of the game object." I don't get why this is happening that when I die and respawn it gives that kind of error output. If there's an alternative script that has better functionality or is suitable/recommended, then please let me know.

What type of script, you ask? It is a Local Script that is in the Tool, and the animation is parented inside the Local Script.

-- Services
local userInputService = game:GetService("UserInputService")
local playersService = game:GetService("Players")

local Player = game.Players.LocalPlayer

-- Variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local pcButton = Enum.KeyCode.LeftShift
local xboxButton = Enum.KeyCode.ButtonL3
local IsEquipped = false

local tool = script.Parent

-- Animation
local animation = script:WaitForChild("SprintAnim")
local sprintTrack = humanoid:LoadAnimation(animation)

-- Input controller
userInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == (pcButton or xboxButton) and not processed and IsEquipped then
		sprintTrack:Play(.1)
	end
end)

userInputService.InputEnded:Connect(function(input, processed)
	if input.KeyCode == (pcButton or xboxButton) and not processed then
		sprintTrack:Stop(.1)
	end
end)

tool.Equipped:Connect(function()
	IsEquipped = true
end)

tool.Unequipped:Connect(function()
	IsEquipped = false
	sprintTrack:Stop(0)
end)

5 comments

0

No, if its a local script it'll automatically redo the script. Before you start the script simply add a wait(2) then it'll work.

0

Just a suggestion, don't use humanoid:LoadAnimation(), it's deprecated. try to loadanimtion using the animator found in the humanoid

0

did @coolfaizan7861's answer work?

1

Yes it worked because him adding that wait and me resetting works just fine.

0

alr cool.

2 answers

✓ Accepted answer

Simply add a wait(1) on the top of the script. Once you reset the script runs instantly and when it does the script breaks because the Humanoid hasn't loaded in yet. so your code would be now

wait(2)
-- Services
local userInputService = game:GetService("UserInputService")
local playersService = game:GetService("Players")

local Player = game.Players.LocalPlayer

-- Variables
local player = playersService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local pcButton = Enum.KeyCode.LeftShift
local xboxButton = Enum.KeyCode.ButtonL3
local IsEquipped = false

local tool = script.Parent

-- Animation
local animation = script:WaitForChild("SprintAnim")
local sprintTrack = humanoid:LoadAnimation(animation)

-- Input controller
userInputService.InputBegan:Connect(function(input, processed)
    if input.KeyCode == (pcButton or xboxButton) and not processed and IsEquipped then
        sprintTrack:Play(.1)
    end
end)

userInputService.InputEnded:Connect(function(input, processed)
    if input.KeyCode == (pcButton or xboxButton) and not processed then
        sprintTrack:Stop(.1)
    end
end)

tool.Equipped:Connect(function()
    IsEquipped = true
end)

tool.Unequipped:Connect(function()
    IsEquipped = false
    sprintTrack:Stop(0)
end)

Your code does not account for respawning.

Its because when you die, respawning is a new character. So you must set the character value everytime the character respawns. Add


Ftredidiid
Dhdjdjd
character = player.Characteradded:wait()
Raucotuufieus
Oagdgbevrjssj
Zhjdhdtrhhsi


Player.CharacterAdded:Connect(function(char)

     character = char
-- also set other variables too, as needed

end)

You could add this or you can simply make a script that goes into the character every time the respawn by using another script. Then you don't have to reset variables, when the character died and respawns, the script doesn't get copied and a new clone of it goes in the character so it sets the variables and does everything else that script does.

Hope this helps :3

This discussion is preserved as it was. New answers, comments, and votes are closed.