Hello. I tried making my own looped script for an animation.
What I'm trying to accomplish is, I have an NPC, and I want it to have an animation that plays and stops for a certain amount of seconds, and then play the animation, stop, wait, and repeat. Here is my script and I'll explain what it should do, or what I think it's doing (first time making my own script)
It's a script in the NPC, and there is an animation in the script too.
repeat while true do local animation = script:WaitForChild('Animation') --finding the animation local humanoid = script.Parent:WaitForChild('Humanoid') --finding humanoid of the npc local dance = humanoid:LoadAnimation(animation) --loading animation dance:Play() --plays wait(20) --I put it to 20 since that's how long the animation is, if I put it to 1 it stops the animation at 1 second and if I put it to 25 it will play the whole animation and loop it for another 5 seconds, which I don't want it looping dance:Stop() --stops the animation after the 20 seconds print("yeah done") --just checking to make sure the above works wait(5) --now this is the amount of time I want the script to wait until repeating and playing the animation end until print("yeah im looping") --im not sure what I was doing here but this doesn't show up in output for some reason wait(0.5)
My problem here is, it works perfectly for the first time, but by the second loop, suddenly the wait time is off, and the animation likes to kind of just, play for 5 seconds restart and play again.
Here's the script again without the -- for easier reading.
repeat while true do local animation = script:WaitForChild('Animation') local humanoid = script.Parent:WaitForChild('Humanoid') local dance = humanoid:LoadAnimation(animation) dance:Play() wait(20) dance:Stop() print("yeah done") wait(5) end until print("yeah im looping") wait(0.5)
Anyone know why this is happening and how I can fix it?
Your “until” part should have a condition after it just like you put “true” in the while do part. Also, I think you shouldn’t be using two exact loops for the problem you’re trying to solve, one might suffice.
Your script should not have repeat and until, because you want it to constantly run, putting that is telling the script to keep looping until it waits half a second. Which is similiar to the "break" feature However, the only difference is the repeat and until breaks the loop when those standards are met.
Take out the repeat and until, your script should look like this:
while true do local animation = script:WaitForChild('Animation') local humanoid = script.Parent:WaitForChild('Humanoid') local dance = humanoid:LoadAnimation(animation) dance:Play() wait(20) dance:Stop() wait(5) -- Change to how long you want the anmiation to stop end
Copy and Paste:
while true do local animation = script:WaitForChild('Animation') local humanoid = script.Parent:WaitForChild('Humanoid') local dance = humanoid:LoadAnimation(animation) dance:Play() wait(20) dance:Stop() wait(5) end
Editors note: For some reason it couldn't space, but thats a simple fix for you, so I'll let you handle it.
That should work. :) Please upvote this post if it is the solution, thank you. I like helping