Playing an animation when a player respawns is not working

I’m very new to scripting so I’m not surprised I’m having trouble with this. but it’s become very frustrating, and I cannot for the life of me figure out what’s happening. The animation does not play upon character spawn, despite “begun” and “played” being printed. “ended” never prints though telling me the function isn’t completing. Firing an incorrectly formatted version of the function results in it playing correctly the first time, but subsequently throwing “Attempt to connect failed: Passed value is not a function” afterwards and refusing to fire the script anymore. Can someone help me troubleshoot this I can’t find any answers for my problem on existing forum posts.

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local spawnanim = Instance.new("Animation")
spawnanim.AnimationId = "rbxassetid://13321429106"
local spawnanimtrack = Animator:LoadAnimation(spawnanim)
spawnanimtrack.Priority = Enum.AnimationPriority.Action
spawnanimtrack.Looped = false
print("gettin there")

local function flipout(lplayer)
	print("begun")
	spawnanimtrack:Play()
	print("played")
	lplayer.Character:WaitForChild("Humanoid").WalkSpeed = 0
	spawnanimtrack.Stopped:Wait()
	lplayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
	print("ended")
	
end

player.CharacterAdded:Connect(function()
	flipout(player)
end)
1 Like

ahh yes, this is because your animation is actually being looped. In order to not loop it you have to either edit the animation in the animator, or play the animation in a server script

You dont need to use this:

player.CharacterAdded:Connect(function()
	flipout(player)
end)

Cause you already waited for the Character to be added in the 3rd line:
local character = player.Character or player.CharacterAdded:Wait()

Try removing that event and just call the function. Once the local script is parented into the Character, the script will run.

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local spawnanim = Instance.new("Animation")
spawnanim.AnimationId = "rbxassetid://13321429106"
local spawnanimtrack = Animator:LoadAnimation(spawnanim)
spawnanimtrack.Priority = Enum.AnimationPriority.Action
spawnanimtrack.Looped = false
print("gettin there")

local function flipout(lplayer)
	print("begun")
	spawnanimtrack:Play()
	print("played")
	lplayer.Character:WaitForChild("Humanoid").WalkSpeed = 0
	spawnanimtrack.Stopped:Wait()
	lplayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
	print("ended")
end

flipout(player) -- run the function
1 Like

Thank you this worked, the logic for the local scripts was slipping past me while writing this lol. Do you know how I can additionally have this play for each time the character spawns after resetting Aswell? the animation doesn’t seem to want to play twice.

Depends where that local script is located.

  • If you place that script in StarterPlayerScripts folder
    When a Player joins your game, the script will be cloned from StarterPlayerScripts and placed in the Player instance > PlayerScripts folder automatically, so the code in the script will run only once, when its given to the player upon join, and the script will persist during whole game session of that player. In that case, you will need the CharacterAdded event in order to know when the player respawned and run the function that animates again.

  • If the script is in StarterCharacterScripts folder
    The script will be cloned and placed in the Character model of the player, and upon dead and respawn the script will be cloned again and placed in the new Character model created. Everytime a Player dies, their Character Model in workspace will be destroyed, including all scripts in it, and when player “respawn”, the engine is creating a new character model, cloning all scripts inside StarterCharacterScripts folder and placed inside the new Character, and the model will be placed in workspace so the player can use it. In that case, your script will be deleted everytime a player dies and they will get a new copy of the script on respawn, so it will run everytime the character respawn.

So, its your choice what behaviour you want for the script, be persistant or be destroyable.

  • If you go for a persistant script in Player, then you need to use the CharacterAdded event.
  • If you go for destroyable script in Character, the code in the script will run when player spawn/respawn
2 Likes

Well the problem is you never load the animation in the character after respawning, so the animation isn’t in the character. I reccomend setting in the CharacterAdded the following:

local function flipout(lplayer)
local spawnanim = Instance.new("Animation")
spawnanim.AnimationId = "rbxassetid://13321429106"
local spawnanimtrack = Animator:LoadAnimation(spawnanim)
spawnanimtrack.Priority = Enum.AnimationPriority.Action
spawnanimtrack.Looped = false
	print("begun")
	spawnanimtrack:Play()
	print("played")
	lplayer.Character:WaitForChild("Humanoid").WalkSpeed = 0
	spawnanimtrack.Stopped:Wait()
	lplayer.Character:WaitForChild("Humanoid").WalkSpeed = 16
	print("ended")
end
1 Like

Ah that’s probably a better place to put it, I had it sitting in the StarterGui lol. Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.