Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

A function to change the animation everytime somebody resets or respawns?

So I have a function to change the walk animation but I'm not sure how to trigger it when a player resets.

here is the function

function addAnimation(player)
    wait(1)
player.Character.Animate.walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/? 
    id=1661650912"
end

I'm not sure if there is an event or how I would figure out if a player reset or respawn. Any help is appreciated :)

2 answers

✓ Accepted answer

use PlayerAdded() and CharacterAdded. PlayerAdded is triggered in the Players when someone is added, and CharacterAdded is triggered when the Player spawns.

function addAnimation(character)
    wait(1)
character:WaitForChild("Animate").walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/? 
    id=1661650912"
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(addAnimation)
end)

I'll explain some other changes I did to your script.

I changed player at line 1 and 3 to character so it would detect the Character directly instead of having to get it through the player.

I changed Animate at line 3 to WaitForChild("Animate") because CharacterAdded fires before the animate script is loaded in. WaitForChild() will make the function wait until the animation script has loaded.

1 comment

0

Thank you kind person this was very explanatory :)

In the CharacterAdded event

local function addAnimation(player)
    wait(1)
    player.Character.Animate.walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=1661650912"
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		addAnimation(Player)
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.