How to make it so that code only runs when the remote event code is finished

Hey! I’m trying to make it so that when the intermission remote event is finished, it will run the game remote event. However upon startup, it just completely skips the intermission event and automatically runs the game remote event. How can I fix this?

Here are the three scripts I am using.

  1. Main Script.
  2. Module.
  3. OnClient.

Is there anything I can include to achieve what I want?
sorry if the scripts are poorly written, im new trying to work on my first mini project :smiley:


local playersInGame = 0

local inRound = false

game.Players.PlayerAdded:Connect(function(player)
	playersInGame = playersInGame + 1
	print(playersInGame)
	player.CharacterAdded:Connect(function(character)
		if playersInGame >= 2 and inRound == false then
			while playersInGame >= 2 do
				if inRound == false then
					roundModule.runIntermission("Intermission: ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", inRound)
				elseif inRound == true then
					roundModule.runGame("Drawer has ", inRound, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120")
				end
			end
			inRound = false
			print(inRound)
		elseif playersInGame < 2 then
			roundModule.updateStatus("Need at least 1 more player to start!")
		end
		
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	playersInGame = playersInGame - 1
	if playersInGame < 2 then
		roundModule.updateStatus("Need at least 1 more player to start!")
	end
end)```

-- module --
local roundModule = {}

local remoteEventRoundStatus = game.ReplicatedStorage:WaitForChild("RoundStatusRemoteEvent")
local remoteEventRoundIntermission = game.ReplicatedStorage:WaitForChild("IntermissionRemoteEvent")
local remoteEventGameStatus = game.ReplicatedStorage:WaitForChild("GameStatusRemoteEvent")

function roundModule.updateStatus(statusMessage)
	game.ReplicatedStorage.Status.Value = statusMessage
	remoteEventRoundStatus:FireAllClients(statusMessage)
end

function roundModule.runIntermission(statusMessage, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen)
	game.ReplicatedStorage.Status.Value = statusMessage
	remoteEventRoundIntermission:FireAllClients(statusMessage, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen)
end

function roundModule.runGame(statusMessage, InRound, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, twentyone, twentytwo, twentythree, twentyfour, twentyfive, twentysix, twentyseven, twentyeight, twentynine, thirty, thirtyone, thirtytwo, thirtythree, thirtyfour, thirtyfive, thirtysix, thirtyseven, thirtyeight, thirtynine, forty, fortyone, fortytwo, fortythree, fortyfour, fortyfive, fortysix, fortyseven, fortyeight, fortynine, fifty, fiftyone, fiftytwo, fiftythree, fiftyfour, fiftyfive, fiftysix, fiftyseven, fiftyeight, fiftynine, sixty, sixtyone, sixtytwo, sixtythree, sixtyfour, sixtyfive, sixtysix, sixtyseven, sixtyeight, sixtynine, seventy, seventyone, seventytwo, seventythree, seventyfour, seventyfive, seventysix, seventyseven, seventyeight, seventynine, eighty, eightyone, eightytwo, eightythree, eighyfour, eightyfive, eightysix, eightyseven, eightyeight, eightynine, ninety, ninetyone, ninentytwo, ninetythree, ninetyfour, ninetyfive, ninetysix, ninetyseven, ninetyeight, ninetynine, onehundred, onehundredone, onehundredtwo, onehundredthree, onehundredfour, onehundredfive, onehundredsix, onehundredseven, onehundredeight, onehundrednine, onehundredten, onehundredeleven, onehundredtwelve, onehundredthirteen, onehundredfourteen, onehundredfifteen, onehundredsixteen, onehundredseventeen, onehundredeighteen, onehundrednineteen, onehundredtwenty)
	game.ReplicatedStorage.Status.Value = statusMessage
	remoteEventGameStatus:FireAllClients(statusMessage, InRound, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, twentyone, twentytwo, twentythree, twentyfour, twentyfive, twentysix, twentyseven, twentyeight, twentynine, thirty, thirtyone, thirtytwo, thirtythree, thirtyfour, thirtyfive, thirtysix, thirtyseven, thirtyeight, thirtynine, forty, fortyone, fortytwo, fortythree, fortyfour, fortyfive, fortysix, fortyseven, fortyeight, fortynine, fifty, fiftyone, fiftytwo, fiftythree, fiftyfour, fiftyfive, fiftysix, fiftyseven, fiftyeight, fiftynine, sixty, sixtyone, sixtytwo, sixtythree, sixtyfour, sixtyfive, sixtysix, sixtyseven, sixtyeight, sixtynine, seventy, seventyone, seventytwo, seventythree, seventyfour, seventyfive, seventysix, seventyseven, seventyeight, seventynine, eighty, eightyone, eightytwo, eightythree, eighyfour, eightyfive, eightysix, eightyseven, eightyeight, eightynine, ninety, ninetyone, ninentytwo, ninetythree, ninetyfour, ninetyfive, ninetysix, ninetyseven, ninetyeight, ninetynine, onehundred, onehundredone, onehundredtwo, onehundredthree, onehundredfour, onehundredfive, onehundredsix, onehundredseven, onehundredeight, onehundrednine, onehundredten, onehundredeleven, onehundredtwelve, onehundredthirteen, onehundredfourteen, onehundredfifteen, onehundredsixteen, onehundredseventeen, onehundredeighteen, onehundrednineteen, onehundredtwenty)
end

return roundModule

-- onclient --

local status = game.ReplicatedStorage:WaitForChild("Status")

local remoteEventRoundStatus = game.ReplicatedStorage:WaitForChild("RoundStatusRemoteEvent")
local remoteEventRoundIntermission = game.ReplicatedStorage:WaitForChild("IntermissionRemoteEvent")
local remoteEventRoundGame = game.ReplicatedStorage:WaitForChild("GameStatusRemoteEvent")

local roundLabel = script.Parent

local intermission

remoteEventRoundStatus.OnClientEvent:Connect(function(statusMessage)
	roundLabel.Text = statusMessage
end)

intermission = remoteEventRoundIntermission.OnClientEvent:Connect(function(statusMessage, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, inRound)
	roundLabel.Text = statusMessage..fifteen
	wait(1)
	roundLabel.Text = statusMessage..fourteen
	wait(1)
	roundLabel.Text = statusMessage..thirteen
	wait(1)
	roundLabel.Text = statusMessage..twelve
	wait(1)
	roundLabel.Text = statusMessage..eleven
	wait(1)
	roundLabel.Text = statusMessage..ten
	wait(1)
	roundLabel.Text = statusMessage..nine
	wait(1)
	roundLabel.Text = statusMessage..eight
	wait(1)
	roundLabel.Text = statusMessage..seven
	wait(1)
	roundLabel.Text = statusMessage..six
	wait(1)
	roundLabel.Text = statusMessage..five
	wait(1)
	roundLabel.Text = statusMessage..four
	wait(1)
	roundLabel.Text = statusMessage..three
	wait(1)
	roundLabel.Text = statusMessage..two
	wait(1)
	roundLabel.Text = statusMessage..one
	wait(1)
	inRound = true
	print(inRound)
end)

remoteEventRoundGame.OnClientEvent:Connect(function(statusMessage, InRound, onehundred, onehundredone, onehundredtwo, onehundredthree, onehundredfour, onehundredfive, onehundredsix, onehundredseven, onehundredeight, onehundrednine, onehundredten, onehundredeleven, onehundredtwelve, onehundredthirteen, onehundredfourteen, onehundredfifteen, onehundredsixteen, onehundredseventeen, onehundredeighteen, onehundrednineteen, onehundredtwenty)
	roundLabel.Text = statusMessage..onehundredtwenty.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundrednineteen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredeighteen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredseventeen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredsixteen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredfifteen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredfourteen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredthirteen.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredtwelve.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredeleven.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredten.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundrednine.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredeight.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredseven.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredsix.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredfive.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredfour.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredthree.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredtwo.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundredone.." seconds remaining"
	wait(1)
	roundLabel.Text = statusMessage..onehundred.." seconds remaining"
	wait(1)
	InRound = false
	print(InRound)
end)

Could you please post the code in text as some of the code can’t be seen in the screenshots, and so that the code is easily read and modifiable?

ah yes my bad ill do that right now

1 Like

So, when players are enough to start (2) you are calling the module for the Intermission function, and you wanna wait until that function is over, to continue with inRound = true, and call the runGame module function?

But, the Intermission function is not waiting at all, it just FireAllClients(), that ends up very quick, the client is who is using the “wait()”, the Intermission function will never wait for something the client is doing

If you want the Intermission function to wait until this is finished:
image

You should run a while loop inside the Intermission function, on each iteration, FireAllClients() sending that data (statusMessage…) In that way, Intermission function will stop the Main script until is finished, and once the loop is over, it will continue with the runGame function in the Main Script

Yeah thats what I’m trying to accomplish.

Can you explain what I need to fix or the problem I have in the script.

I edited my previous message with a possible approach you could use

Your Intermission function should look something like this, so it will repeat 19 times then it breaks, and let the Main Script to continue with the tasks

An example:

Main Script

local module = require(script.Parent:WaitForChild("ModuleScript"))

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		warn("run intermission")
		local isIntermissionEnd = module.Intermission()
		if isIntermissionEnd then
			warn("run game")
		end
	end)
end)

The Module

local module = {}

module.Intermission = function()
	local waitTime = 0
	while waitTime < 20 do
		--TheRemote:FireAllClients(TheMSG)
		warn(waitTime)
		waitTime += 1
		wait(1)
	end
	return true
end

return module

On each iteration you could fire the remote for all players, to send something

The output, that shows that the script waited until Intermission was over:
image

OHHH that makes a lot more sense. Thank you! I’m probably just going to take the time to understand what all the code means.

Also, instead of just putting each number as a paramater, is there another way to do this such as using the waitTime variable? Or like a for loop?

roundModule.runIntermission("Intermission: ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", inRound)```

Yup, those numbers are the timer to start right? and goes backwards? first 15, then 14 etc?

Use the loop to send that data to client:

The module

local module = {}

module.Intermission = function()
	local waitTime = 1
	local Timer = 15
	while waitTime <= 15 do
		TheRemote:FireAllClients(Timer)
		warn(waitTime)
		waitTime += 1
		Timer -= 1
		wait(1)
	end
	return true
end

return module

On Client:

TheRemote.OnClientEvent:Connect(function(theMSG)
      TextLabel.Text = "Time left to Start: "..tostring(theMSG)
end)

You probably wanna play a little with those numbers, maybe turn the 1 into 0 or change the 15 to 16

ahhh ty so much for taking the time to help. i just learned a lot :smiley:

1 Like

No problem, I really like to help, and I learn a lot too when trying to help xD

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.