Animation Code not working

My code just wont work

When the function is called it runs all the way through with no errors.
But animation is never played

(The animations are the defult roblox ones)

(Tried local and server scripts in StaterCharacterScripts)

local AnimationOne = Instance.new("Animation")
AnimationOne.AnimationId = "rbxassetid://130018893"

local AnimationTwo= Instance.new("Animation")
AnimationTwo.AnimationId = "rbxassetid://132546839"

local AnimationThree = Instance.new("Animation")
AnimationThree.AnimationId = "rbxassetid://132546884"

local Animations = {AnimationOne, AnimationTwo, AnimationThree}

local AnimationTrack
local Dancing = false
local Humanoid = script.Parent:WaitForChild('Humanoid')
local Animator = Humanoid:WaitForChild("Animator")

function Dance()	
	Dancing = true
	AnimationTrack = Animator:LoadAnimation(Animations[math.random(1,#Animations)]) -- this will return an AnimationTrack instance that can be played
	AnimationTrack:Play()
end

Dance()

have you set the character to R6?

The problem might be that you didn’t set a parent for the new instance ( the animations parent = nil )
I don’t think it works like that, BUT I could def be wrong and It works for some whack reason.

Maybe try setting the parent to ServerScriptService or the humanoid

so i tested something and the script only works in a localscript. and also the AnimationTrack is not Looped = true so it only plays like 2 seconds.

Okay this fixed it, turns out i was using R6 animations Whoops.

Another problem though is that when it plays and the player moves the animation stops, which is fine

although im trying to get it so they keep dancing after they stop again.

So im using this

while true do
	repeat wait() until game.Workspace.Players:WaitForChild(game.Players:GetPlayerFromCharacter(script.Parent).Name).InDance.Value == true
	Dance()
	repeat wait() until AnimationTrack.IsPlaying == false
end

But i dont think when the animation is cancelled by moving that IsPlaying == false. its still playing but not showing (I think)

How can i fix this

(Thanks for the help so far)

set the animationtrack to loop
https://developer.roblox.com/en-us/api-reference/property/AnimationTrack/Looped

do you want it so if the player pressing a butten the random dance is playing?
and if he start moving the dance will stop?

Yeh so there’s a server value Called InDance. when that is == True the player is in a constant dance state untill InDance == false.

i guess he could be dancing while hes moving or the animation pause while hes moving and continues when he stops again.

Ive tried AnimationTrack.Looped = true but that hasnt made a difference :confused:

So it works imediatly when InDance == true but when he moves he stops dancing and because IsPlaying is still equal to true for some reason he doesnt continue his lil dance.

All i really need is a way to detect if the animations been cancelled without making a load of functions to tell if WASD is being pressed

so you press a button and he is start dancing. if he is start moving the dance stop?

(its a little bit harder for me to understand everything because i am German lel)

Yes

And when he Move again the dance Plays IF InDance == true

is the dance starting with keyboard input or with a text button
?

Neither

Its when a player stands in a region. But don’t worry I’ve got that code sorted
InDance == True when he is in Region
and InDance == false when hes out of region

Dont worry about it @Geruchsneutral44 Turns out i just needed to set Priority to 1

Thanks for the help.