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

how to break a module script that is running a function?

Asked by 5 years ago

okay so i am making a mini-game place that uses module scripts for the mini-games this is the main script

spawn(function()require(Randomlyselectedmodulescript)(Playing)end)
wait(1)
start = tick()
while wait() do
    if tick() - start >= 180 then
        break
    end
end
--break/end the module script

this is a test module script

return function(Players)
    while true
        --do minigame stuff
    end
end)

how would i get the break/end the module script after 3 minutes without putting the wait 3 mins in the module script any help appreciated

0
facts: 3 minutes = 180 seconds, so you would do wait(180) DeceptiveCaster 3761 — 5y
0
without putting the wait 3 mins in the module script, also it will detect if the players die etc. so i need to have it loop instead if that is what you meant ForgotenR4 68 — 5y
0
oh, then u simply can't DeceptiveCaster 3761 — 5y
0
Don't use while wait() do. You're clearly missing the point of the conditional portion of the loop. Adding an if statement to check the terminating condition in order to break is unacceptable. User#24403 69 — 5y

2 answers

Log in to vote
1
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago
Edited 5 years ago

I suggest instead of doing while true do, you instead listen to a variable that changes by a bindable event.

Eg.

local MinigameRunning
script.BindableEvent.Event:Connect(function() -- use BindableEvent:Fire() to stop the loop.
    MinigameRunning = false
end)

return function()
    MinigameRunning = true -- set to true so loop starts
    while MinigameRunning do -- loop until the bindableevent sets it to false
    --stuff
    end
end)

Ad
Log in to vote
0
Answered by 5 years ago

Neither bindable events or spawn functions should be used here.

Coroutines are there for a reason, you can run code on a separate thread and yield it or resume it whenever you want.

local wrap = coroutine.create(function()
    require(Randomlyselectedmodulescript)(Playing)

    wait(1)
    start = tick()
    while wait() do
        if tick() - start >= 180 then
            coroutine.yield()
        end
    end
end

coroutine.resume(wrap )
--break/end the module script
0
'spawn function shouldnt be used here' -> uses coroutine which is literally the same as spawn RubenKan 3615 — 5y
0
... Spawning functions will run in a separate thread with a function, breaking will not end the function only returning it. Courotines can be yielded, resumed, etc.. Please learn when to use certain methods over other ones, and stop throwing shade. Thanks! NodeSupport 32 — 5y

Answer this question