I need to make a system that picks a random game to setup for players to play. Everyone always rants “DONT USE WHILE TRUE DO” but I really can’t think of any other way that is efficient. I will be calling each mini game by their separate modules.
Why would you need a loop for this system?
while true do
is fine to use if it solves your problem.
People do advocate against using wait()
(task.wait()
or RunService events are better options), which might be what you’re thinking of.
Round based system I don’t really know what else to do
I don’t see why I would need run service to run every frame for an intermission and game system
Give me your layout of what your going to do to acomplish this. A loop can easily be avoided.
I am trying to create minigames, it randomly picks a mini game then calls the module to play that said minigame.
Why exactly would you need a loop for this? I don’t see a use for a loop here.
Rather than using a loop for that, because it’d be looping and doing nothing for a long time, or uselessly yielding the looped script, just use bindeableEvents
for something like that. Have your main script choose a game through a function, and then have it fire the minigame. In the minigames script/module, have it fire an end function, and in your main program, have either an intermission function, or just the gamestarter function fire when it recieves a signal from the game over event.
Alternatively, a solution that could be seen as “better” (I don’t know why, I’m just throwing around an idea), is recursion. Rather than do while true do
, have a function which runs and at the end of the function it can just, for example, wait for a return value from the minigame module it fired, then once it gets a return, the following line can call the function again, something like this:
local function runGame()
--Code to pick game and/or intermission stuff
local gameOver = --fire a function in the minigame module and wait for a return.
--Alternatively, if it's not a module, fire a bindeableFunction and wait for a return on that
--Obviously, that bindeableFunction would run the minigame chosen.
--Recur:
runGame()
end
You don’t, I’m saying that a while loop is fine.
local Server = game:GetService("ServerStorage")
local Value = Server.Value --BoolValue instance.
local function DoRound() --Example function which starts a single round.
Value.Value = true --Set the BoolValue instance to true to indicate that a round is ongoing.
--Have round code here.
Value.Value = false --Set the BoolValue instance to false to indicate that a round has ended.
end
Value.Changed:Connect(function(InRound)
if not InRound then --If not in round.
DoRound() --Start a new round.
end
end)
Here’s a very primitive script (just an example) which should demonstrate how you can achieve a rounds system without necessitating a loop. I’ve added comments to the script to further explain how it operates.
How do I communicate from my module when to end the round? I don’t want it to end until my minigame module tells it too
Use a BindableEvent. Essentially you fire the event and another script can act when they see the event triggered. You can use this to begin cleaning up the minigame and such.