Hey guys. Got a bit advanced question!
Let me paste the script first Script \/
while true do wait(5) if #game.Workspace.Lobby:GetChildren() >= 1 then TeleportPlayers() wait(1) spawn(function() GameModules.Mode1() repeat wait(1) until #game.Workspace.InGame:GetChildren() == 1 and GameModules.GameOver == true Surviver = game.Workspace.InGame:FindFirstChildOfClass('StringValue') print('Stopped ',Surviver) end) elseif #game.Workspace.Lobby:GetChildren() <= 2 then print('Not enough players in-Lobby (',#game.Workspace.Lobby:GetChildren(),')') end end
ModuleScript \/
local GameModes = {} GameOver = false function GameModes.Mode1() print('Mode1') spawn(function(CountDown) for i=6,0,-1 do wait(1) print(i,' Seconds remaining') end end) wait(15) end return GameModes
I want to normal script to wait UNTIL the ModuleScript has finished (after waiting 15 seconds) before the script continues.
I attempted this by doing 'until #game.Workspace.InGame:GetChildren() == 1 and GameModules.GameOver == true'
Is it not waiting because of the Spawn(Function())?
GameOver can only be accessed from the Modulescript because you did not add it to the table that the module returns. Because of this, GameModules.GameOver will always == nil in the first script. In addition, the variable is never changed, anyways. You can make GameOver accessible to other scripts by changing it to this:
GameModes.GameOver = false
One thing to keep in mind about Modulescripts is that they only run once and then return a cached table whenever they are required, so if this causes your GameOver variable to get stuck (been there done that :/) you could do something similar using BindableEvents
Script:
while true do wait(5) if #game.Workspace.Lobby:GetChildren() >= 1 then TeleportPlayers() wait(1) -- having this in a separate thread might cause weird behavior because the thread will be waiting while the game is running, but the rest of the script will still be running, so I got rid of the spawn() since there isn't any reason for this to be in a separate thread anyways GameModules.Mode1() --with BindableEvents, the "Event" event happens when the BindableEvent is fired. Instead of connecting to it, we can use :Wait() to halt the thread until it is fired. game.ReplicatedStorage.GameOverEvent.Event:Wait() Surviver = game.Workspace.InGame:FindFirstChildOfClass('StringValue') print('Stopped ',Surviver) elseif #game.Workspace.Lobby:GetChildren() <= 2 then print('Not enough players in-Lobby (',#game.Workspace.Lobby:GetChildren(),')') end end
Module:
local GameModes = {} Module.GameOver = false function GameModes.Mode1() print('Mode1') for i=6,0,-1 do wait(1) print(i,' Seconds remaining') end wait(15) game.ReplicatedStorage.GameOverEvent:Fire() end return GameModes