How to make two seperate spawnpoint types (solved)

@Ovibion I think I fixed it, I tested it and it seemed to work properly. Let me know if anything is messed up. And you’re 100% right about me needing to make my code more organized, 9/10 times I have a bug and a simple change of code location in the script tends to fix it.

local Players =  game:GetService("Players")
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)

Edit: nvm its still broken

Wait I fixed it. I forgot to fix the player in the table.insert and table.remove since roblox didnt do it automatically. I’m 95% sure it works right now

Great. Also, do you need to filter the spawns? Are there other parts inside their folders?

Nope, just spawns in those folders

Then you can remove that filtering code and reduce the lines in your script; you’re basically repeating the same code in the two table.find() if statements.

local Players = game:GetService("Players")

local lobby, battlegrounds = {}, {}
local lobbySpawns = workspace.LobbySpawns:GetChildren()
local battlegroundSpawns = workspace.BattlegroundsSpawns:GetChildren()

local switchPart = workspace["Switch To Battlegrounds Part"]

local debounce = false

Players.PlayerAdded:Connect(function(player)
    table.insert(lobby, player)

    player.CharacterAdded:Connect(function(character)
        local spawns = if table.find(lobby, player)
                then lobbySpawns
                else battlegroundSpawns
        local randomSpawn = spawns[math.random(#spawns)]
        
        -- Model:SetPrimaryPartCFrame() is deprecated.
        character.HumanoidRootPart.CFrame = randomSpawn.CFrame
    end)
end)

switchPart.Touched:Connect(function(hit)
    local player = Players:GetPlayerFromCharacter(hit.Parent)

    if not player or debounce then
        return
    end

    debounce = true

    task.delay(5, function()
        debounce = false
    end)

    table.remove(lobby, table.find(lobby, player))
    table.insert(battlegrounds, player)
end

Also, here’s a link to the Lua style guide that you can follow when you write scripts, since you are a beginner: Lua Style Guide. You may be able to grow from this site, as well: Luau-lang.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.