KeyCode animation wont work after respawn

I’m making a game called Kick, where you kick people off of a player chosen map and I got the script working so that when you press ‘E’ the animation plays but whenever someone respawns they’re unable to kick again (also i haven’t set up other players being hurt or thrown back from the other players kick)

local UserinputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait() or player.Character
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local debounce = false

local function onInBegan(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E then
		local shockAnimation = Instance.new("Animation")
		shockAnimation.AnimationId = "rbxassetid://8200368678"
		
		if not debounce then
		
			debounce = true
			
			local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
			shockAnimationTrack.Priority = Enum.AnimationPriority.Action
			shockAnimationTrack.Looped = false

			shockAnimationTrack:Play()
			humanoid.WalkSpeed = 0
			wait(shockAnimationTrack.Length)
			humanoid.WalkSpeed = 16
			
			wait(1)

			debounce = false
		end
	end
end
print(character)

UserinputService.InputBegan:Connect(onInBegan)

You are referencing an animator that, when removed, is still contained in the variable and the loaded animation is related to that animator, you must update the variable

local UserinputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local debounce = false

local shockAnimation = Instance.new("Animation")
shockAnimation.AnimationId = "rbxassetid://8200368678"

local shockAnimationTrack, humanoid = nil
local function LoadAnimation(character)
	if not character then			return			end
	humanoid = character:WaitForChild("Humanoid")
	
	local Animator = humanoid:WaitForChild("Animator")
	shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
	shockAnimationTrack.Priority = Enum.AnimationPriority.Action
	shockAnimationTrack.Looped = false
end
LoadAnimation(player.Character)
player.CharacterAdded:Connect(LoadAnimation)

local function onInBegan(input, gameProcessed)
	if input.KeyCode ~= Enum.KeyCode.E or debounce then		return		end
	debounce = true
	
	shockAnimationTrack:Play()
	humanoid.WalkSpeed = 0
	task.wait(shockAnimationTrack.Length)
	humanoid.WalkSpeed = 16
	task.wait(1)
	debounce = false
end
UserinputService.InputBegan:Connect(onInBegan)

when adding a character, the script will update shockAnimationTrack which is the loaded animation, you could also use this and place it in StarterCharacterScripts

local UserinputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local debounce = false

local shockAnimation = Instance.new("Animation")
shockAnimation.AnimationId = "rbxassetid://8200368678"

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = (character):WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")


local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
shockAnimationTrack.Priority = Enum.AnimationPriority.Action
shockAnimationTrack.Looped = false

local function onInBegan(input, gameProcessed)
	if input.KeyCode ~= Enum.KeyCode.E or debounce then		return		end
	debounce = true
	
	shockAnimationTrack:Play()
	humanoid.WalkSpeed = 0
	task.wait(shockAnimationTrack.Length)
	humanoid.WalkSpeed = 16
	task.wait(1)
	debounce = false
end
UserinputService.InputBegan:Connect(onInBegan)

does the same, but does not need to be updated.

1 Like