How to make a randomized spawn system? (Solved)

local player = game.Players
local LobbyTable = {}

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)]
			char:SetPrimaryPartCFrame(spawnLocation.CFrame)			
		end
	end)
end)


I tried this, but it didnt work. Am I just stupid or is there a reason this isnt working?

You’ve put the Players service, try changing game.Players to game:GetService(“Players”):GetPlayers()

Fixed it

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)
1 Like

Okay for some reason when players touch the part they aren’t spawning at BattlegroundsSpawns


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)

local defaultSpawn = workspace.Part -- Your default spawn part, in case any errors occurs.

local spawnPoints = {
	["spawnpoint of the sea"] = {
		instance = workspace.SpawnPoint, -- Part which the player will be teleported to.
		chance = 20
	}
}

function spawnUser(player) -- user: player instance
	local totalChance = 0
	-- Calculates the total chance of all spawns, if the .chance is nil, adds 10 as the chance.
	for _, values in pairs(spawnPoints) do
		if values["chance"] == nil then -- Making sure that every single spawn has a chance.
			values["chance"] = 10
		end
		totalChance = totalChance + (values.chance)
	end
	
	local random_number = math.random(1, totalChance)
	local chanceWeight = 0
	local chosen_spawnPoint
	
	-- Sorts a random item on the spawnpoints list based on the total chance.
	for spawnPart, itemValues in pairs(spawnPoints) do
		chanceWeight += itemValues.chance
		if random_number <= chanceWeight then
			chosen_spawnPoint = itemValues
			break
		end
	end
	
	-- Moves the character to the desired position.
	local character = player.Character or workspace:WaitForChild(player.Name)
	character:MoveTo(chosen_spawnPoint.instance and chosen_spawnPoint.instance.Position or defaultSpawn.Position)
	print("Teleported player to spawn "..chosen_spawnPoint.instance.Name)
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		spawnUser(player)
	end)
end)