Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

4 comments

0

facts: 3 minutes = 180 seconds, so you would do wait(180)

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

0

oh, then u simply can't

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.

2 answers

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)

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

2 comments

0

'spawn function shouldnt be used here' -> uses coroutine which is literally the same as spawn

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.