Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with spawning in the correct sequence?

I have 15 spawns in my game, each named "1" through "15", respectively, stored in a Folders object named SpawnLocations.

This is the corresponding code:

local REQUIRED_PLAYERS = 1

game.Players.PlayerAdded:Connect(function(player)

for n, player in pairs(game.Players:GetPlayers()) do
	local playerTable = {}
	local n = 1
	
	playerTable[n] = player
	n = n + 1
	end	

if game.Players.NumPlayers == REQUIRED_PLAYERS then
	for n = 1, 1 do
		player:LoadCharacter()
		player.Character.PrimaryPart.Position = game.Workspace.SpawnLocations[tostring(n)].Position
	end
end

end)

The problem that arises, is that when I go to test this via server, the player joins, but spawns at spawn #6, instead of spawn #1. Also, his head falls off.

I am unsure where the code in incorrect, but I'm willing to hear all suggestions.

1 answer

✓ Accepted answer

You didn't describe exactly what the code is supposed to do, so I made a guess:

local Players = game:GetService("Players")
local SpawnLocations = workspace.SpawnLocations
local i = 1

Players.CharacterAutoLoads = false

Players.PlayerAdded:Connect(function(Player)
	Player.RespawnLocation = SpawnLocations[i] -- Assign new Player to a Spawn
	i = i + 1
	Player:LoadCharacter()
end)

3 comments

0

Thank you for this answer. Works as I wanted it to. I must have overthought the spawning process, you made it look so simple. Thanks again.

0

Yup! No problem. Roblox has a lot of things built-in to make life easier! And for things that aren't built-in, I've written some modules for those. Let me know if you need any more help! Add me on Discord: Niles#8328

0

You also might want to make an adaptive system that accounts for when players leave the game, and a new one takes their place. The simple version I wrote for you only accounts for players entering and staying in the game. Send me your finalized code when you've completed it!

This discussion is preserved as it was. New answers, comments, and votes are closed.