How would someone set up a Remote Event and a Timer?

It’s a bit complicated. What I’m trying to do is have a few players push a GUI button within a 15 second countdown. I’ll draw a picture:

I have my code set up just like this, however, I’m confused on where to put the Remote Event that is coming from #2 (where the client clicks the button in time – while a countdown is still going).

I also have one last question. How would it be possible to, when the server receives all the clicks (assuming everyone clicked the button in time), for the timer to stop and break out of its loop?

1 Like

For steps 1 and 2, you can just use the same remote event. The server fires to the client to open up the gui, and using that same remote, the server receives that the client clicked the button. On the server for step 2 remote one method would be on the countdown an empty playersClicked = {} table is created. And on the server event for the step 2 remote the player will be added to that table.

As for breaking out of the loop, you can check if all the players in the server are stored in the playersClicked table, and if they are, break out of the loop using the break statement.

2 Likes

Thank you! If you don’t mind, could you give me an example of a countdown being done while the client is sending a remote event back to the server (as seen on #2)?

1 Like

Here is something I just whipped up:

local countDownRemote = BLAH

countDownRemote:FireAllClients() --Tells all clients that are currently in the game to open the the button GUI

local playersClicked = {}

local connection = countDownRemote.OnServerEvent:Connect(function(player)
	-- The server receives this function whenever a player clicks the button
	if not table.find(playersClicked, player) then
		table.insert(playersClicked, player)
	end
end)

local startTime = tick()

repeat
	RunService.Heatbeat:Wait()
	if #playersClicked >= #game.Players:GetPlayers() then
		break
	end
	--This waits for 15 seconds or until all players clicked
	--Do timer things here
until (tick() - startTime) > 15

connection:Disconnect() -- Turns off the remote event so that the server receives no more click events

--You can now have your game code here, and you have the players that's ready to play in the playersClicked table.
2 Likes

Yep. I would’ve have done something completely different. Thank you for the example!

1 Like