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)
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