How to make two seperate spawnpoint types (solved)

So I’m making a game with a similar lobby system to Zo. I’m going to set it up so when a player is added to a game it’ll immediately add them to a table called “LobbySpawn”. Then when they go through the Teleporter to the actual map it’ll add them to a different table called “BattlegroundsSpawn” and remove them from the “LobbySpawn” table.

Then I’m going to use something similar to this (link below) for having randomized spawn points. I’m going to use the tables so it’ll only spawn them at certain spawns depending on the table they’re in. However I would need to set up two different types of a spawns for this. Is there a way I can label Lobby Spawns & Battlegrounds Spawns and only have a player spawn at a specific spawn type depending on the table they’re in? (No I’m not using team service nor will use it)

1 Like

Yes, you can label the spawn points by creating two separate spawn point objects and renaming them as “LobbySpawn” and “BattlegroundsSpawn”. You can then write a script to check which table the player is in and spawn them accordingly.

So like

void Start () {
  if (GameObject.Find("LobbySpawn").GetComponent ().players.Contains(gameObject)) {
    SpawnAtLobby();
  }
  else if (GameObject.Find("BattlegroundsSpawn").GetComponent ().players.Contains(gameObject)) {
    SpawnAtBattlegrounds();
  }
}

void SpawnAtLobby() {
  // spawn player at LobbySpawn
}

void SpawnAtBattlegrounds() {
  // spawn player at BattlegroundsSpawn
}

So literally all I gotta do is rename the spawn point thing?

How would implement it with this - How to Make Players spawn at a random Spawn - #2 by LoafingHour

Would I change SpawnPoints:GetChildren() to SpawnPoints:GetChildren(“Battlegrounds Spawn”) for example?

You’d create Folders for the spawn points (one named “LobbySpawns” and the other “BattlegroundSpawns”), and then you’d call Instance:GetChildren() on both of them. For example, this:

local lobbySpawns, battlegroundSpawns = -- [Location #1, Location #2]
lobbySpawns:GetChildren()

Or you can just store the children of those folders as the variables:

local lobbySpawns = --[[Folder location here]]:GetChildren()
2 Likes

(Adding on to the reply above)
If you want to get a random spawn, you would do
edit: fixed

folder = --[[insert folder]]
spawn = folder:GetChildren()[math.random(1,#folder:GetChildren())]
1 Like

Okay I need to revive this rq. @Ovibion @MrFergxl @TheSai_ki

So we have 2 tables, one named LobbyTable, one named BattlegroundsTable. Then we have 2 folders with spawn locations inside of them. These folders are called Lobby Spawns & Battlegrounds Spawns, the spawn locations inside those folders are named after the folder they’re in (LobbySpawn & BattlegroundSpawn). When a player first joins they’re added to the LobbySpawn table, I want to set it up so if they’re in that table they’ll only spawn at spawn locations named LobbySpawn. Then the rest is easy, when they touch a specific part they’re removed from the lobbyspawn table and added to the battlegroundstable. Then if they’re in the BattlegroundsTable they’ll only spawn at spawn locations named BattlegroundSpawn.

I need help figuring out how to check if a player is in a table (for this example let’s use the LobbyTable). Then if they’re in that table they’ll only spawn at spawn locations with that name (Following the example - if a player is in LobbyTable then they’ll only spawn at spawn locations named LobbySpawn).

It isn’t necessary to name the SpawnLocations or spawn parts, since they’re in folders that distinguish them.

If you want to check if a player is in a specific table, you can type this:

table.find(array, player)

This method will return nil if the player isn’t in the table, or will return an index of where the player is in that table. Check if it returns nil or not.

If the “spawn locations” are just BaseParts, you can index a random spawn.

local lobbySpawns = --[[Folder location]]:GetChildren()

local randomSpawn = lobbySpawns[math.random(#lobbySpawns)]

Then place the player on that spawn after their character spawns or the player changes teams.

However, you may be able to set the player’s Player.RespawnLocation if the “spawn locations” are literal SpawnLocations. If you do this, you would probably have to reload the character afterwards using Player:LoadCharacter(), or do this when the Humanoid.Died event fires. (Note: you’d have to create a new Humanoid.Died event for every new character`.)

You can set up a Player.CharacterAdded for the player, then do the checks and spawning in there. Here’s an example snippet:

player.CharacterAdded:Connect(function(character)
    local spawns = if table.find(lobby, player)
        then lobbySpawns
        else battlegroundSpawns

    local randomSpawn = spawns[math.random(#spawns)]

    -- Place the character on the spawn.
end)

I managed to somewhat figure it out. The lobby spawns are working perfectly. And even when I touch the part I’m removed from the tables and everything correctly. But then when a player respawns after touching the part they aren’t respawning at the correct spawns. I made a print statement to check if the else statement is running and it is, so what am I doing wrong? And yes I’m using the actual roblox SpawnLocations.


local player = game.Players
local LobbyTable = {}
local BattlegroundsTable = {}
local SwitchTablePart = game.Workspace["Switch To Battlegrounds Part"]


game.Players.PlayerAdded:Connect(function(player)
	table.insert(LobbyTable, player)
	print("Player added to the lobby table")
	player.CharacterAdded:Connect(function(char)
		if table.find(LobbyTable, player) then
			print("Player located in lobby table")

			local spawnFolder = game.Workspace:FindFirstChild("LobbySpawns")	
			local spawnLocations = spawnFolder:GetChildren()
			local filteredLocations = {}	

			for i, location in ipairs(spawnLocations) do
				if location.Name == "LobbySpawn" then
					table.insert(filteredLocations, location)
				end
			end	

			local spawnLocation = filteredLocations[math.random(1,#filteredLocations)]
			task.wait()
			char:SetPrimaryPartCFrame(spawnLocation.CFrame)			
		else
			if table.find(BattlegroundsTable, player) then
				print("Player located in battlegrounds table")

				local spawnFolder = game.Workspace:FindFirstChild("BattlegroundsSpawns")	
				local spawnLocations = spawnFolder:GetChildren()
				local filteredLocations = {}	

				for i, location in ipairs(spawnLocations) do
					if location.Name == "BattlegroundsSpawn" then
						table.insert(filteredLocations, location)
					end
				end	

				local spawnLocation = filteredLocations[math.random(1,#filteredLocations)]
				task.wait()
				char:SetPrimaryPartCFrame(spawnLocation.CFrame)		
				end
		end
	end)
end)


local Debounce = false 
SwitchTablePart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if Debounce == false then
			Debounce = true
			print("Player added to battlegrounds table, user will no longer spawn in lobby")
			table.insert(BattlegroundsTable, player)
			print("Player successfully added to battlegrounds table")
			table.remove(LobbyTable,table.find(LobbyTable,player))
		    print("Player successfully removed from lobby table")
			wait(5)
			Debounce = false
		end
	end
end)

It seems that you’re inserting the Players service into the BattlegroundsTable, instead of a player. At the top of your script, you defined player as game.Players.

Use the Players:GetPlayerFromCharacter() method to insert and remove the players from the tables when they touch that part.

local player = Players:GetPlayerFromCharacter(hit.Parent)

-- Insert and remove the player here.

And define a Players variable at the top, and prefer to use that variable rather than game.Players for the PlayerAdded event.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    -- ...
end
1 Like

Nvm I fixed it.

local player = game.Players
local LobbyTable = {}
local BattlegroundsTable = {}
local SwitchTablePart = game.Workspace["Switch To Battlegrounds Part"]
local Debounce = false 

game.Players.PlayerAdded:Connect(function(player)
	table.insert(LobbyTable, player)
	print("Player added to the lobby table")
	player.CharacterAdded:Connect(function(char)
		if table.find(LobbyTable, player) then
			print("Player located in lobby table")

			local spawnFolder = game.Workspace:FindFirstChild("LobbySpawns")	
			local spawnLocations = spawnFolder:GetChildren()
			local filteredLocations = {}	

			for i, location in ipairs(spawnLocations) do
				if location.Name == "LobbySpawn" then
					table.insert(filteredLocations, location)
				end
			end	

			local spawnLocation = filteredLocations[math.random(1,#filteredLocations)]
			task.wait()
			char:SetPrimaryPartCFrame(spawnLocation.CFrame)			
			
		elseif table.find(BattlegroundsTable, player) then
				print("Player is located in the battlegrounds table spawn system")
			local spawnFolder = game.Workspace:FindFirstChild("BattlegroundsSpawns")	
			local spawnLocations = spawnFolder:GetChildren()
			local filteredLocations = {}	

			for i, location in ipairs(spawnLocations) do
				if location.Name == "BattlegroundsSpawn" then
					table.insert(filteredLocations, location)
				end
			end	

			local spawnLocation = filteredLocations[math.random(1,#filteredLocations)]
			task.wait()
			char:SetPrimaryPartCFrame(spawnLocation.CFrame)	
		end
		
		
		SwitchTablePart.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				if Debounce == false then
					Debounce = true
					table.insert(BattlegroundsTable, player)
					print("Player successfully added to battlegrounds table")
					table.remove(LobbyTable,table.find(LobbyTable,player))
					print("Player successfully removed from lobby table")
					wait(1)
					Debounce = false
				end
			end
			
		end)
	end)
end)

I’ll probably make an open source thing or tutorial since this could be useful for a lot of people

You should not put the BasePart.Touched event inside of the Player.CharacterAdded event. If you do this, the player will be added to the BattlegroundsTable when other players touch the part. And that will cause an issue with your code.

1 Like

I tested it using a two player local server. Works just fine for me

i take that back, it didnt. ill fix that

What I am saying is that there might be duplicates of the same player in the BattlegroundsTable.

It’s not good practice for you to set up event listeners inside of other event listeners if the event listener is something that can be used outside of the other event listener.
This can cause threading issues, and may even affect performance.

You should keep your code as neat and organized as possible.

Edit: Also, I just saw your last message.

1 Like

where do I put this? local player = Players:GetPlayerFromCharacter(hit.Parent)

yep u were right, idk why I didnt think of that

In the BasePart.Touched event. Use this to add and remove the player from the arrays.

local Players =  game:GetService("Players")
local player = Players:GetPlayerFromCharacter(hit.Parent)
local LobbyTable = {}
local BattlegroundsTable = {}
local SwitchTablePart = game.Workspace["Switch To Battlegrounds Part"]
local Debounce = false 

Players.PlayerAdded:Connect(function(player)
	table.insert(LobbyTable, player)
	print("Player added to the lobby table")
	player.CharacterAdded:Connect(function(char)
		if table.find(LobbyTable, player) then
			print("Player located in lobby table")

			local spawnFolder = game.Workspace:FindFirstChild("LobbySpawns")	
			local spawnLocations = spawnFolder:GetChildren()
			local filteredLocations = {}	

			for i, location in ipairs(spawnLocations) do
				if location.Name == "LobbySpawn" then
					table.insert(filteredLocations, location)
				end
			end	

			local spawnLocation = filteredLocations[math.random(1,#filteredLocations)]
			task.wait()
			char:SetPrimaryPartCFrame(spawnLocation.CFrame)			

		elseif table.find(BattlegroundsTable, player) then
			print("Player is located in the battlegrounds table spawn system")
			local spawnFolder = game.Workspace:FindFirstChild("BattlegroundsSpawns")	
			local spawnLocations = spawnFolder:GetChildren()
			local filteredLocations = {}	

			for i, location in ipairs(spawnLocations) do
				if location.Name == "BattlegroundsSpawn" then
					table.insert(filteredLocations, location)
				end
			end	

			local spawnLocation = filteredLocations[math.random(1,#filteredLocations)]
			task.wait()
			char:SetPrimaryPartCFrame(spawnLocation.CFrame)	

		end
	end)
end)



SwitchTablePart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if hit.Parent:FindFirstChild("Humanoid") then
		if Debounce == false then
			Debounce = true
			table.insert(BattlegroundsTable, player)
			print("Player successfully added to battlegrounds table")
			table.remove(LobbyTable,table.find(LobbyTable,player))
			print("Player successfully removed from lobby table")
			wait(1)
			Debounce = false
	   end
	end	
end)

for some reason its not working anymore, how do I fix it?