Dumb question about spawn logic

Going to write out a small snippet in order to help explain my question.

local chosen = {}
local players = game.Players:GetPlayers()
for _,player in pairs(players) do
table.insert(chosen, player)
end

Then, after all is said and done, I loop through the Chosen table and assign each player to the – for sake of explanation – “Playing” team. When I go to respawn each player, however, I notice that each user spawns in the same location, despite the fact that I’ve a multitude of spawns throughout the map.

Realistically, I want each player to spawn at their own spawn location. Each spawn should be occupied by one player, not two, not three, not four.

Is there a way for me to make that work? Because I keep ending up with two players spawning at the same location.

Thanks, friends!

1 Like

I dont see how thhis code affects spawning of players, but anyways u used table.insert wrong. table.insert(urTable, position(I always do 1), value).

Calling it with two arguments is just an overload for it and it is just fine.

3 Likes

You could use a loop and manually set the CFrame of each player with something like

game.Workspace.imalex4:SetPrimaryPartCFrame(game.Workspace.spawnpoint1.CFrame * CFrame.new(0, 6, 0))

You may need to change the 6 if your spawn block is thick or if you have tall players.

Here is the script, i have explained everything so you wouldn’t get confused :slight_smile: :

local Spawners = game.Workspace:FindFirstChild("Spawners"):GetChildren() --// This should be a group, containing the BaseParts so players can get teleported in one of them. It will return a table with the spawners
local Players = game:GetService("Players"):GetPlayers() --// This will return a table with all the players.
for _, Player in pairs(Players) do --For each player, it will do...
    local Char = Player.Character or Player.CharacterAdded:Wait() --// It will get the Character of the Player.
    local RandomSpawn = Spawners[math.random(#Spawners)] --// It will get a random spawner.
    Char:SetPrimaryPartCFrame(RandomSpawn.CFrame) --// The Character's Humanoid Root Part's CFrame will be set as the RandomSpawn CFrame, aka teleport.
    table.remove(Spawners, table.find(RandomSpawn)) --// The thing you wanted, this will remove the spawner that was selected from the table Spawners, so the random logic thing will not get it anymore.
end

If you get any issue or an error you can tell me and i might fix it. Thanks for reading.

1 Like

Thanks for your response, man! I received an error because of this line:

Char:SetPrimaryPartCFrame(RandomSpawn.CFrame)

[20:32:15.918 - ServerScriptService.Script:26: invalid argument #1 (table expected, got Instance)]

Are you sure that’s the correct line? Because i don’t see that a table is required in that line. I think this is the correct line:

table.remove(Spawners, table.find(RandomSpawn))

Here i fixed it:

table.remove(Spawners, table.find(Spawners, RandomSpawn))

Also, remember that Spawners must not be a part, it must be a model.

1 Like

Yeah, I get it now. Looking back on it, I feel silly that I couldn’t figure it out. I really appreciate your help! Thanks a ton.