Whenever I loop a function using while, it just doesn’t work at all
Try some of these examples and read through this page:
I know how to make loops, just the function is not working with the loop.
Here’s the small code I have
while task.wait(2) do
startGame()
end
Can you please be more descriptive? What do you mean by looping functions? Are you using a while loop to continuously call the function? Can you show us the code?
You can see above, sorry for no clarification
Then the problem is your function, if it doesn’t run.
What are you asking? Loops work.
while task.wait(2) do
print("something")
end
Prints “something” every two seconds.
Which is really confusing since it runs without a loop. Not sure how to fix it, I can show you the function if you want
My function does not work while in a loop, but it does outside of one
You got the whole idea wrong.
It works without a loop because you call it, a while loop’s job in this case is calling the function continuously, but you said the function won’t run so your function is the culprit.
I see now, but I’m still unsure how to fix it. I can give you the code if you can help
game.Players.PlayerAdded:Connect(function()
if gamestatus.Value == "waiting for players" and #game.Players:GetChildren() >= minPlayers and #game.Teams.Players:GetPlayers() <= winPlayer then
task.wait(2)
setupGame()
gamestatus.Value = "Starting soon"
while task.wait(2) do
events.chooseRandomEvent()
if #game.Teams.Players:GetPlayers() == winPlayer then
clearPlates()
gamestatus.Value = game.Teams.Players:GetPlayers()[1].Name .. " has won"
end
end
end
end)
end
Why are you using a while loop to continually check the event? The event is continuously checking for updates so you don’t have to use a function that continuously calls the event.
After the first round it can’t go again
What is this supposed to be doing?
Module script, chooses a random event from a library of events
So is that while task.wait() loop not working or is it the other one above that (the one with start game)? I’m confused as they don’t seem to be at all related.
The while task.wait() loop, no errors
Does the if statement run when you want it to? It’s probably something with the function definitions.
Just noticed something, this is halting the script. Probably because interval is stated because of an error but not sure how to fix it (in the module script, events)
local randomPlate = plates[math.random(1,#plates)]
Why not take a loop out of the equation and just use an event-driven approach?
local GameRunning, CanStart = false
local function StartGame()
if GameRunning then
return
end
GameRunning = true
...
GameRunning = false
CanStart()
end
function CanStart()
if #game.Players:GetPlayers() >= 2 then
StartGame()
-- else
-- GameRunning = false
end
end
game.Players.PlayerAdded:Connect(CanStart)
–
With the code I supplied, it runs the CanStart
function again to re-start the game, thus “looping” it.