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