Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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.

01repeat
02    while true do
03        local animation = script:WaitForChild('Animation') --finding the animation
04        local humanoid = script.Parent:WaitForChild('Humanoid') --finding humanoid of the npc
05        local dance = humanoid:LoadAnimation(animation) --loading animation
06        dance:Play() --plays
07        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
08        dance:Stop() --stops the animation after the 20 seconds
09        print("yeah done") --just checking to make sure the above works
10        wait(5) --now this is the amount of time I want the script to wait until repeating and playing the animation
11    end
12until
13print("yeah im looping") --im not sure what I was doing here but this doesn't show up in output for some reason
14wait(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.

01repeat
02    while true do
03        local animation = script:WaitForChild('Animation')
04        local humanoid = script.Parent:WaitForChild('Humanoid')
05        local dance = humanoid:LoadAnimation(animation)
06        dance:Play()
07        wait(20)
08        dance:Stop()
09        print("yeah done")
10        wait(5)
11    end
12until
13print("yeah im looping")
14wait(0.5)

Anyone know why this is happening and how I can fix it?

2 answers

Log in to vote
1
Answered by 4 years ago

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 — 4y
0
Do my method it works. ifreakinlostmyacount 52 — 4y
0
Just use only while true do for the infinite loop. ArtFoundation 255 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years 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:

01while true do
02    local animation = script:WaitForChild('Animation')
03    local humanoid = script.Parent:WaitForChild('Humanoid')
04    local dance = humanoid:LoadAnimation(animation)
05 
06    dance:Play()
07    wait(20)
08    dance:Stop()
09    wait(5) -- Change to how long you want the anmiation to stop
10 
11end

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 — 4y
1
You need to measure the exact time or use something like wait(animation.TimeLength) ifreakinlostmyacount 52 — 4y
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 — 4y
1
Well the second wait just needs to be wait(5) SORRY FOR NOT RESPONDING! ifreakinlostmyacount 52 — 4y
0
Yeah I have it as that, but my animation is now just continuously looping instead of stopping and waitng Aprilsaurus 48 — 4y

Answer this question