Spawn/Teleport Positions Parts

I am trying to set a part for each player to be teleported to when the game starts. I have a folder of 15 parts where I want the players to spawn. The max player amount is 15. However I have this script here and I attempted to get the value to be true if the player is teleported to it, but it just sets all the values under all the parts in the folder to true. I know that it is looping but I am not sure how to reference the part outside of the loop.

	for i, part in pairs (teleportSpawns:GetChildren()) do
		if part:FindFirstChild("Occupied").Value == false then

			for _, v in pairs(P_S:GetChildren()) do
				local character = v.Character
				local HRP = character:WaitForChild("HumanoidRootPart")
				v.Character:MoveTo(part.Position)
			end
		end
		part.Occupied.Value = true
	end

I am not sure if this is the best way to do this task either. If anyone knows how I can reference the part outside the loop or have any ideas on how I can complete the task please let me know.
Thanks.

P_S is

game:GetService("Players")

What if you move that line under:

1 Like

still sets every value parented to the parts (all the parts) to true, it needs to be outside the loop im thinking

You dont need to check if they are occupied. Instead, make a variable for the table of parts to teleport, and a table of players:

local parts = teleportSpawns:GetChildren()
local players = game.Players:GetPlayers()

You then iterate the players table, setting their positions to the part position:

local offset = Vector3.new(0,5,0)
for i=1,#players do
local pos = parts[i].CFrame+offset
players[i].Character.HumanoidRootPart.CFrame = pos
end

If you want the players positions to be chosen randomly, and for every part to have a chance of being used as a teleport position, you will want to do some type of shuffle function on the parts table every time you want to start a new game.