0

# My looped script stops working correctly the first time. How to make a script loop correctly?

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
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')
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?

1

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.

0
How would I do that? I tried researching but I'm trying to make the whole script repeat infinitely. Aprilsaurus 48 — 1y
0
Do my method it works. ifreakinlostmyacount 52 — 1y
0
Just use only while true do for the infinite loop. ArtFoundation 255 — 1y
1
Edited 1 year ago

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')

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

0
It still does the same problem. It's like the wait(20) gets bugged by the second loop. Aprilsaurus 48 — 1y
1
You need to measure the exact time or use something like wait(animation.TimeLength) ifreakinlostmyacount 52 — 1y
0
Ah, okay that works but it loops the animation, so now the dance:Stop() no longer works, oof imma do a little more research on how to stop animation loops, but if you know how to please tell me ;-;, but thank you for helping! Aprilsaurus 48 — 1y
1
Well the second wait just needs to be wait(5) SORRY FOR NOT RESPONDING! ifreakinlostmyacount 52 — 1y
0
Yeah I have it as that, but my animation is now just continuously looping instead of stopping and waitng Aprilsaurus 48 — 1y