Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Animation script stop working when I die/respawn?

Asked by
TechModel 118
3 years ago

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)
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. coolfaizan7861 20 — 3y
0
Just a suggestion, don't use humanoid:LoadAnimation(), it's deprecated. try to loadanimtion using the animator found in the humanoid Gmorcad12345 434 — 3y
0
did @coolfaizan7861's answer work? JesseSong 3916 — 3y
1
Yes it worked because him adding that wait and me resetting works just fine. TechModel 118 — 3y
0
alr cool. JesseSong 3916 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

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)
Ad
Log in to vote
0
Answered by 3 years ago

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

Answer this question