How can I know when a game starts instead of using 'waits'

Ohhh I think I know what you meant now about the remote. I thought you meant that you want to find something else to replace the wait() so erm. From what I’ve seen is that whenever player join the game the script will instantly start running even when the player does not loaded yet.

I suggest you make a loop just to check if the player exist before starting the game like

repeat wait(1) until #game.Players:GetPlayers() >= 1
print("Hello World!")
--> Wait until 1 or more player exist then print Hello World!
print("Hello World")
--> Instantly print Hello World once the server start running without waiting for the player to exist at first

Apology for misunderstanding.

Edited: And since you’re making a game with timer I suggest you make a loop inside a loop! Heres an example of what I usually do if I want to make a game that require at least one player before it starts. Unless you want the game to only run once I suggest to only loop it once instead of loop-ception

function StartGame()
	while true do
		game.ReplicatedStorage.Remote:FireAllClients()
		-- Loop timer until it breaks
	end
end

while wait(1) do
	if #game.Players:GetPlayers() >= 1 then
		StartGame()
	end
end
4 Likes

It works for the first player but not for others? Do I probably have to wait for the actual Local Player?

if you want it to work with the player that joins the game late then I suggest you use PlayerAdded function and check if the game is running, if so fire the remote to the players

game.Players.PlayerAdded:Connect(function(Player)
    -- Check if the game is running
    -- Fire remote for new player that joins the game
end)
1 Like

‘game.Players.PlayerAdded’ doesn’t work because each time a new player joins it will run the code right? I can’t use the ‘PlayerAdded’ function because my code has everything made for the minigame, timer, stages, other functions.

Then you should tried to set the main variable once the game started? heres an example of the timer that has been set once the game started

local TimeRemaining

function StartGame()
	TimeRemaining = os.time() + 180 --> 180 seconds remaining
	while wait(1) do
		game.ReplicatedStorage.RemoteEvent:FireAllClients(TimeRemaining)
		-- Loop and stuff
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	game.ReplicatedStorage.RemoteEvent:FireClient(Player, TimeRemaining)
end)

while wait(1) do
	if #game.Players:GetPlayers() >= 1 then
		StartGame()
	end
end
1 Like

I wonder If I could store the local player’s name when he joins in a string value and then get that player or something? The thing is that the timer does not affect anything in the script, it’s just that the #game.Players:GetPlayers() >= 1’ function only waits for 1 random player to join the game and then runs the code, but what if I want to wait for the specific local player in my main server script?

1 Like

What do you mean by specific local player in my main server? Do you mean like if someone name Test123 join the game and the event will start?

Not quite, I mean when you as a player join the game the server script will run, right? I tested with my builder and he joined the game first the remote fired and everything worked fine, when I joined after him the remote did not fire because the script no longer needed to wait for 1 player and just ran before I even joined which did not run the remote event for me.

sry for the explanation I am not really good at that xD. I meant when any local player joins the game it will work fine and fire the remote event.

Like waiting for the local player to load in, instead of using #game.Players:GetPlayers() >= 1’ maybe like use #game.Players:GetPlayers(localPlayer)’ or something like that.

1 Like

Because the FireAllClient function has already fired for all of the players within the server which is your builder in that case and started the game. After you joined the game the remove doesn’t fire because it has already passed the FireAllClient statement.

Just like the previous explanation PlayerAdded should do the trick since the remote only fire once the game started and not after it.

1 Like

But what if I can’t use the PlayerAdded function is there another way the fix the problem? Because what if my script has a round generating code in it, each time a new player join’s it will run it and glitch the game out.

Even when they are not the first player to joinI think its because the code is running too fast and not waiting for the player to load in fully before running.

Perhaps use Player.CharacterAdded instead then.

API Reference: Player | Documentation - Roblox Creator Hub

1 Like