This script is supposed to teleport all of the player from the table, however the last player in the table does not get teleported
This part is working, it prints both players in the table
local plrs = {}
game.ReplicatedStorage.AddPlayerToTable.OnServerEvent:Connect(function(player)
--for i, player in pairs(plrs) do
--if not player then
table.insert(plrs,player)
print(plrs)
--end
--end
end)
In this part of the script it also prints both players but the last player in the table does not get looped through the for loop
if #plrs == 2 then
print(table.unpack(plrs))
local map = game.ReplicatedStorage:FindFirstChild(“Arena”)
local clonedMap = map:Clone()
clonedMap.Parent = workspace
local SpawnPoints = clonedMap:FindFirstChild(“SpawnPoints”)
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
table.remove(AvailableSpawnPoints,1)
table.remove(plrs,i)
game.ReplicatedStorage.WaitScreenEnd:FireClient(player)
else
if not player then
table.remove(plrs,i)
end
end
end
end
end
I’d say let’s rewrite this code and make it easier to work with. Remember to make it clear what each snippet of your code does!
I don’t know why you’re having the client tell the server to add them to the plrs table, but I won’t do that in the following code.
My version
function SpawnMapAndTeleportPlayers(Map)
local Players = game.Players:GetPlayers()
-- I don't fully know your intentions so I won't check for if the Character exists, this is up to you to add if necessary
--|| Clone and parent map
Map = Map:Clone()
Map.Parent = workspace
--|| Teleport players
local SpawnPoints = Map.SpawnPoints:GetChildren()
for i, player in pairs(Players) do
local spawn = SpawnPoints[i] -- assuming that there are enough spawn points for each player
-- Teleport player
end
end
If you want me to help you out further, then I’ll need you to explain what exactly you’re wanting to do and what you need help with.
What I’m trying to do: The player picks a character to play as, then they wait (there’s a wait screen) for more players to join. Once there are 5 players they get teleported to the map (wait screen goes away).
Problem: The 6th player does not get teleported and is still in the wait screen.
I used your script and changed it a little
local plrs = {}
game.ReplicatedStorage.AddPlayerToTable.OnServerEvent:Connect(function(player)
--for i, player in pairs(plrs) do
--if not player then
table.insert(plrs,player)
--end
--end
end)
function SpawnMapAndTeleportPlayers(Map, player)
Map = game.ReplicatedStorage.Arena:Clone()
Map.Parent = workspace
local SpawnPoints = Map.SpawnPoints
local AvailableSpawnPoints = SpawnPoints:GetChildren()
for i, player in pairs(plrs) do
if player then
local character = player.Character
if character then
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
table.remove(AvailableSpawnPoints,1)
table.remove(plrs,i)
game.ReplicatedStorage.WaitScreenEnd:FireClient(player)
end
end
end
end
while true do
if #plrs == 2 then
SpawnMapAndTeleportPlayers()
end
wait()
end
You’re still using table methods, though. Which I specifically tried not to use
In addition, you added if player then. This serves no purpose as it’d always be true considering the context. If you’re wanting to check if the player exists, then you need to check if its parent is nil.
You just need to be a bit creative. I misunderstood your question apparently, but to check how many players there are, you can just make a function like so:
function GetTableLength(Table)
local Length = 0
for i, v in pairs(Table) do
Length += 1
end
return Length
end
P.S. I’m trying not to make it all for you, but helping you with parts of it. I’m on purpose not putting everything together.
It’s skipping some players in the list because you are making the list shorter while looping through the same list.
local t = {"A","B","C","D","E"}
for i,v in pairs(t) do
if v == "C" then
table.remove(t, i)
end
print(i,v)
end
Output:
1 A
2 B
3 C
4 E
See how there was 5 entries, but it only prints 4 times?
What you need to do is instead of removing players from the list, to simply just teleport as you go down the list
if character then
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[i].CFrame
game.ReplicatedStorage.WaitScreenEnd:FireClient(player)
end