I would do all animations on the Client, as the Client controls their character and if you try and do animations from the server, it can come out really weird and buggy.
Something that I think might resolve your issue is, make a LocalScript inside of StarterPlayerScripts and have the animation play on the character, once the character is loaded in. Scripts inside of StarterPlayerScripts, only run once and that is when the Player First Joins the game.
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid : Humanoid = Char:WaitForChild("Humanoid")
local Animator : Animator = Humanoid:WaitForChild("Animator")
task.wait() -- just a slight buffer to fully recognize your character has loaded
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://AnimationID"
local loadedAnimation = Animator:LoadAnimation(anim)
loadedAnimation:Play()
script:Destroy() -- It's not needed anymore, might as well get rid of it
If you already have the “Animation” instance, you can just reference that in your code instead of creating a new one and setting it’s id, the animation still needs to be loaded though.
I have my respawn code that play animation inside the ServerScriptService
if I want to add this to the player will it overwrite the code I put in there
or I’ll need to do something about it
If you were to stop all animations inside that Script, it could prevent the animation from playing, but otherwise I don’t think it will be an issue.
If the issue comes to be something like overlapping animations, you could also try changing the AnimationPriority on the Animations, and give the Join animation a higher priority than the other.
I put a localscript inside StarterCharacterScripts and it working fine but when the animation finished it will continue the animation I put inside respawn script in the ServerScriptService
is there a way to prevent that?
Try adding this after the task.wait() and before the Animation being created in the LocalScript:
for index, animationTrack in pairs(Animator:GetPlayingAnimationTracks()) do
animationTrack:Stop()
end
Ideally, this would just stop all current animations being played on the character, and then the new animation would play and after that, all should go back to normal.