My animations work fine until the character dies, then I get this error. Is this a roblox bug?
My code:
--!strict
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator") :: Animator
local Tool: Tool = script.Parent
local Cooldown: number = Tool:GetAttribute("Cooldown")
local Animations = Tool:WaitForChild("Animations")
local SwingAnimation = Animations:WaitForChild("Swing") :: Animation
local SwingAnimationTrack = Animator:LoadAnimation(SwingAnimation)
local onCooldown = false
local function OnActivate()
if Tool:GetAttribute("OnCooldown") then return end
SwingAnimationTrack:Play()
end
Tool.Activated:Connect(OnActivate)
I have tried yielding until the character exists right before defining the animation track, but that also did not work.
I’ve had this issue before, assuming that the script is under the tool. What seems to happen is that the tool gets added to the backpack while the previous character still exists (may be a roblox bug who knows)
You can test this out with this script
local player = game.Players.LocalPlayer;
local character = player.Character or player.CharacterAdded:Wait();
wait(3);
print(character.Parent);
So the solution in theory could be to either wait until the first time the tool is equipped to define the character, or to place the script elsewhere (in starter character scripts), or check if the humanoid is alive and loop until it is.
1 Like
I’ve tried yielding until the humanoid is alive, loading the animation on equip, and loading the animation on activate. None of them worked. I managed to sort of get it working by redefining the character, humanoid, animator, swing animation, and the animation track on equip, its not a solution but its a step in the right direction.
Tool.Equipped:Connect(function()
Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
Humanoid = Character:WaitForChild("Humanoid")
Animator = Humanoid:WaitForChild("Animator") :: Animator
local Animations = Tool:WaitForChild("Animations")
local SwingAnimation = Animations:WaitForChild("Swing") :: Animation
SwingAnimationTrack = Animator:LoadAnimation(SwingAnimation)
end)