How can I detect when 2 players join at the same time?

So I have a game where when a player joins it automatically assigns them an available character. The script works, but when 2 players join at the exact same time it only assigns one of them a character. This is a problem because I can’t test it out on roblox studio, because server testing makes all players spawn in at the exact same time.

Basically, I want the players to wait until all other players are done being automatically assigned, or I just want to prevent them spawning at the exact same time.

2 Likes

How are you assigning your characters? Is it a model you have saved in ReplicatedStorage? A script for us to view would be beneficial.

1 Like

So basically when you click a character on your client it will send a message to the server to change your character to that.

So to automatically assign them, I put a server script that loops through all the characters (Right to left) and find the closest available one to the left. Then It sends a message to the client with the available character. The client then sends the message back to the server the same way it would if you were to click on that character.

2 Likes

You can use os.time(), and see if they joined/respawned within the same second.

os.time() returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00), under current UTC time. If provided a table formatted similarly to that returned by os.date , it will return the number of seconds since that time instead.

More information:

1 Like

Well I’m confused on how I can implement that into my script?

When a player joins do you want me to record the os.time and then all other players have to wait until os.time is slightly higher? I don’t understand how os.time works anyway after I read it.

1 Like

I’m confused what you mean by “but when 2 players join at the exact same time it only assigns one of them a character”.

You might want to fix that issue instead of adding complicated checks to avoid it.
How are you assigning the character?

I explained how I assigned the character in one of the replies above.

The script to assign them has to be in a server script, so that’s why when 2 players join it can only do one at a time. I just want it to wait until all players are done being automatically assigned before doing the next player.

1 Like

What method are you using to change their character?

The same method it would be if they were to click on one of them. Only the character is chosen by an AI.

1 Like

Ever considered to look into this?
https://developer.roblox.com/en-us/api-reference/property/Player/CharacterAppearanceId

This should solve your issue as you are not really providing us the method you are using (in a script).

The script does work, just not only when 2 players join at the exact same time, because it can only serve 1 player at a time.

1 Like

Without any code, I cannot help you.

I already told how it works. The reason I didn’t show you is because the script is sloppy because I’m a new scripter and I didn’t want your attention to be focused on that.

But here is the script anyways, it is readable. Also yes the game is about fnaf.

local Players = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local RS = game:GetService("ReplicatedStorage")
local characterClicked = RS.CharacterClicked
local text = game.Workspace.ScrollingGUI.Animatronics.ScrollingFrame.Claimed

Players.PlayerAdded:Connect(function(plr)
	wait(1)
	local backpack = plr.Backpack
	local characterChosen = backpack.CharacterChosen
	local available = nil
	
	if text["Foxy"].Visible == false then
		available = "Foxy"
	end

	if text["Chica"].Visible == false then
		available = "Chica"
	end

	if text["Bonnie"].Visible == false then
		available = "Bonnie"
	end

	if text["Freddy"].Visible == false then
		available = "Freddy"
	end

	if text["Puppet"].Visible == false then
		available = "Puppet"
	end

	if text["Balloon Boy"].Visible == false then
		available = "Balloon Boy"
	end

	if text["Mangle"].Visible == false then
		available = "Mangle"
	end

	if text["Toy Chica"].Visible == false then
		available = "Toy Chica"
	end

	if text["Toy Bonnie"].Visible == false then
		available = "Toy Bonnie"
	end

	if text["Toy Freddy"].Visible == false then
		available = "Toy Freddy"
	end

	if text["Night Guard"].Visible == false then
		available = "Night Guard"
	end
	
	local availableEvent = RS.Available
	availableEvent:FireClient(plr, available)
end)

I know I could of used a table, but I’m not good at that kind of stuff yet. Just know that it works.

1 Like

Any errors?
And I’m pretty sure you don’t need the wait(n).

What’s in your OnClientEvent script?

Nah, there are no errors. It’s just able to only serve 1 player at a time so that’s why it only works for one player when 2 join at the same time.

I also put the wait(1) there because the backpack takes about 1 second to load and it would return an infinite yield.

And here is the client script:

local availableEvent = RS.Available

availableEvent.OnClientEvent:Connect(function(available)
    characterChosen.Value = available
    characterClicked:FireServer(available)
end)

It does the same thing it would do if you were to click on it, but the character you clicked is replaced with “available” which is the character the AI automatically assigned.

1 Like

Why are you just sending the available back to the server? There are no changes being made to it.

Because remote events can’t go from server to server. It also needs to know which player to assign.

1 Like

You should consider using a BindableEvent, in other words:
Server → Server
Client → Client

I tried using a bindable event but it didn’t work because the server can’t get the local player.

Also that’s not the point. The reason it doesn’t work is because the server can only serve 1 player at a time when they join. I just want to know how it can wait for all players to be done before serving the next one.

1 Like

Why do you need the localplayer?