Give every player a different spawn location

Hellow Fellow developers!

I want to give every player, that gets added to the game, a different spawnLocation.

Example: Every time a player gets added to the game, a box gets cloned. The player has to spawn in that cloned box to not be with the other players.

I have no idea where to start.

Is there even a way to make it work? :thinking:

Thanks in advance for your time and help!

1 Like

Do you mean that the spawn locations gets spawned by scripts that can be spawned only for a certain player and not others or you make a lot of different spawn locations that can be spawned only for a certain player and not others?

In short, do you mean that you spawn Spawn Locations by scripts for only one player or you make a lot of spawn locations for only one player.

2 Likes

I believe something like this would work, if I understand your request.

local Players = game:GetService('Players')

local SpawnLocations = workspace.SpawnLocations:GetChildren() --get an array of possible spawns

Players.PlayerAdded:Connect(function(Player)
	Player.RespawnLocation = table.remove(SpawnLocations, math.random(#SpawnLocations)) --set their RespawnLocation to a random spawn
end)

Players.PlayerRemoving:Connect(function(Player)
	table.insert(SpawnLocations, Player.RespawnLocation) --insert the leaving player's RespawnLocation back into the array
end)

This works because table.remove returns the element it removed so we can simply use that to remove from the array and set their RespawnLocation at the same time.

2 Likes

When a player gets added to the game, a spawnlocation gets cloned somewhere. This should be the spawnlocation of the player, but sometimes the player spawns on someone else’s spawnlocation.

1 Like

Thank you for your help. I’ll try to use this when i can. If it works, i’ll mark it as a solution :wink:

1 Like

It works perfectly!!! Thank you again for your time and help, I appreciate it!