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.
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.
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.
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.
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.
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.
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.
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.