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

How to assign each player their own spawn points when they're teleported into a map?

My goal is to teleport players whenever the countdown ends, and while there are no problems with that, there seems to be a problem regarding player spawn points. Each player is assigned a spawn point depending on what kind of index the player has in the table (the table is updated each round, it's all happening in the OnClientEvent, which in turn gets called by a loop once the countdown ends)

However, once I decided to bring someone to test the game, I couldn't help but notice that both me and tester were teleported to the same spot, the spot where player[1] should be teleported, despite the fact that there are 2 players, not 1.

As I decided to delve deeper into this problem, I reckon it's because of the IF condition that I used, but I can't exactly pinpoint the problem. This issue existed before, and I thought that adding GetChildren() function into the event would fix the problem, but it didn't. Script: (viewing the source is advised)

game.ReplicatedStorage.Wait.OnClientEvent:Connect(function()
	local players = game.Players:GetPlayers()
		if players[1] then
				players[1].Character:MoveTo(Vector3.new(-691.75, 1.5, 2181.85))
			elseif players[2] then
				players[2].Character:MoveTo(Vector3.new(-733.7, 1.5, 2181.85))
			elseif players[3] then
				players[3].Character:MoveTo(Vector3.new(-775.45, 1.5, 2181.85))
			elseif players[4] then
				players[4].Character:MoveTo(Vector3.new(-421.15, 1.5, 1901.43))
			elseif players[5] then
				players[5].Character:MoveTo(Vector3.new(-381.6, 1.5, 1901.43))
			elseif players[6] then
				players[6].Character:MoveTo(Vector3.new(-322.35, 1.5, 1901.43))
			elseif players[7] then
				players[7].Character:MoveTo(Vector3.new(1039.78, 1.52, 1923.129))
			elseif players[8] then
				players[8].Character:MoveTo(Vector3.new(1039.78, 1.52, 1973.379))
			elseif players[9] then
				players[9].Character:MoveTo(Vector3.new(1039.78, 1.52, 2013.579))
			elseif players[10] then
				players[10].Character:MoveTo(Vector3.new(245.941, 1.47, 1951.26))
			elseif players[11] then
				players[11].Character:MoveTo(Vector3.new(289.491, 1.47, 1951.26))
			elseif players[12] then
				players[12].Character:MoveTo(Vector3.new(329.441, 1.47, 1951.26))
		end 

1 comment

0

The real problem is that you're using elseif statements, becuase if there's one player the other one won't be teleported too.

1 answer

So you can make a server limit of people, like 12, its the server limit you have. The problem is that you're using elseif statments and not if statements. If there's one player (obviously there would be), then you can't run the other elseif statements teleporting players to other spawnpoints.

What you can do is have 12 spawnpoints on the workspace, do a for loop and change the name to a player spawn point, see:

local spawnpoints = game.Workspace.Spawnpoints:GetChildren()
game.ReplicatedStorage.Wait.OnClientEvent:Connect(function()
       local players = game:GetPlayers() -- Gets the players from the game. --
       for i, v in ipairs (spawnpoints) do
              for index, player in ipairs(players) do
             		if i = index then
                              v.Name = player.Name -- Assigns a player name to the spawnpoint --
			end					
		end
	end
       for i, v in ipairs(spawnpoint) do
              for index, player in ipairs(players) do
			if v.Name = player.Name then		
				game.Workspace[player.Name].HumanoidRootPart:MoveTo(v.Position + Vector3.new(0,3,0) -- Moves to the position with an extra height to not get stuck.
			end
		end
	end
end)

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