I have a few animations that play after a user presses a specific key. The problem is that only about half of them work after the player dies, and the 'roll' animation is only playable twice until it doesn't register the key at all. No errors show up in the output, what should I do?
Roll animation code:
wait(-1)
local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.KeyCode == Enum.KeyCode.Q and debounce == false then
debounce = true
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://123"
local Track = Humanoid:LoadAnimation(Animation)
Track:Play()
local BodyV = Instance.new("BodyVelocity")
BodyV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BodyV.Velocity = Player.Character.HumanoidRootPart.CFrame.lookVector * 25
BodyV.Parent = Player.Character.HumanoidRootPart
wait(1)
BodyV:Destroy()
Track.Stopped:Connect(function()
attacking = false
debounce = false
end)
else
end
end)
Downward smash animation code: (works after player dies, infinite amount of times)
wait(-1)
local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.KeyCode == Enum.KeyCode.E and debounce == false then
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
debounce = true
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://456"
local Track = Humanoid:LoadAnimation(Animation)
Track:Play()
Track.Stopped:Connect(function()
attacking = false
wait(3)
debounce = false
end)
else
end
end
end)
Flurry punch code: (works regularly before player dies, after that it's a simple roll of the dice)
wait(-1)
local UserInputService = game:GetService("UserInputService")
local debounce = false
local attacking = false
UserInputService.InputBegan:Connect(function(InputObject)
if InputObject.KeyCode == Enum.KeyCode.X and debounce == false then
debounce = true
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://789"
local Track = Humanoid:LoadAnimation(Animation)
Track:Play()
Track.Stopped:Connect(function()
attacking = false
wait(3)
debounce = false
end)
else
end
end)
Not asking for any new code, just asking for possible causes of this weird phenomenon and any edits I could potentially make. Thanks in advance, sorry if the code it a bit messy.

3 comments
^There's a couple more scripts but I didn't want to make it needlessly longer than it already was, everything included is in a separate localscript inside of starterplayerscripts
Use a PlayerAdded and CharacterAdded event, and place the script inside. It would fire everytime the player or character is added to the game.
@MArzal It's inside a LocalScript so I can't use playeradded. I'll check to see if characteradded works though