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

Repeat block of code within a repeat?

Asked by
nicros 165
9 years ago

title might not make sense :P

so ive got this script

script.Parent.ClickDetector.MouseClick:connect(function(player)
    local leftMouseDown = true
    player:GetMouse().Button1Up:connect(function() 
        leftMouseDown = false 
    end)
    wait(1)

    while leftMouseDown do
        --Below needs to repeat every 2 seconds
        local animationTrack = player.Character.Humanoid:LoadAnimation(script.FishingAnimation)
        animationTrack:Play()
        --Above needs to repeat every 2 seconds
        player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 10 
        wait(math.random(1,10))
    end
end)

ive got my animation that plays on repeat along with my give gold live which both repeat between every 1 to 10 seconds, problem is the the animation is only 2 seconds long, so if the wait is 3 seconds the animation stops for 1 second which i cant have...

so my question is how would i get the animation to repeat every 2 seconds no matter what and the gold + 10 to repeat every 1 to 10 seconds like it is now? if this is even possible

any and all help is appreciated thanks

2 answers

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
9 years ago

I would suggest using the spawn function.

What it lets you do is run code somewhat concurrently, as if you were running it on a new script. This allows you to do things like loops within loops that aren't affected by each other (exactly what you want).

It's simple to use spawn. It accepts a function argument, which it executes. You can set it up like so...

spawn(function()
    -- code here
end)

For example, this is valid code...

spawn(function()
    while wait(3) do
        print("three seconds passed!")
    end
end)

spawn(function()
    while wait(5) do
        print("five seconds passed!")
    end
end)

Both of those loops will run simultaneously as expected.

0
oh cool thanks for helping me out :) nicros 165 — 9y
Ad
Log in to vote
1
Answered by
Discern 1007 Moderation Voter
9 years ago

You can use the spawn function, which creates a new thread in a script that runs at the same time as other threads in a script. This is also known as a coroutine.

spawn() takes one parameter, which is the function that you want to run. Using spawn to call the function will multi-thread it and allow it to run at the same time as other threads inside the same script.

function playAnimation(player) -This is the function that would be running at the same time as the other threads inside this script.
        while leftMouseDown do
                local animationTrack = player.Character.Humanoid:LoadAnimation(script.FishingAnimation)
                animationTrack:Play()
                wait(2)
        end
end

script.Parent.ClickDetector.MouseClick:connect(function(player)
    local leftMouseDown = true
    player:GetMouse().Button1Up:connect(function() 
        leftMouseDown = false 
    end)
    wait(1)

    spawn(playAnimation(player)) --Call the function we created earlier using the spawn function, player being the player who clicked the button.
    while leftMouseDown do
        player.leaderstats.Gold.Value = player.leaderstats.Gold.Value + 10 
        wait(math.random(1,10))
    end
end)

You can read more about the spawn function here.

You can read more about coroutines here.

If I helped you out, be sure to accept my answer!

0
appreciate the help :P nicros 165 — 9y

Answer this question