SpawnPoint problem

Hi Guys, I have a problem with my script. The players aren’t teleporting to the spawnpoints, can’t figure out what the problem could be but here is the code.

local SpawnPoints = ClonedMap:FindFirstChild(“SpawnPoints”)

if not AvailableSpawnPoints then
print(“no spawnpoints cuh”)
end

AvailableSpawnPoints = SpawnPoints:GetChildren()

Status.Value = “Get ready to play”

wait(1)

for i, player in pairs(plrs) do
if player then
character = player.Character

	if character then
		

		character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
		table.remove(AvailableSpawnPoints,1)

I think it could be AvailableSpawnPoints that is the problem but no output message comes out.

4 Likes

Is this the entire script? I found some missing variables and as far as I know the variable AvailableSpawnPoints is referenced in an if statement even though it is not cached

I changed it up a bit for this line of code, and that was not the entire script. Is this right as I have referenced SpawnPoints and AvailableSpawnPoints, I’ll see if it works

local AvailableSpawnPoints = ClonedMap:FindFirstChild(“SpawnPoints”)

if not AvailableSpawnPoints then
print(“no spawnpoints cuh”)
end

SpawnPoints = AvailableSpawnPoints:GetChildren()

1 Like

Still dosen’t work, Where would I reference AvailableSpawnpPoints

2 Likes

hold on I’m rewriting your script ill also explain so you get a better overview

2 Likes

Ask me the things you don’t understand ill answer you appropriately

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local AvailableSpawns = ClonedMap:WaitForChild("SpawnPoints"):GetChildren()
local SpawnTables = {}

--Adding AvailableSpawns children to a table
for i, v in pairs(AvailableSpawns) do
	table.insert(SpawnTables, v)
end

--Caches the function to help wtih performance
local function tpToSpawn(Character)
	local HumanoidRootPart = Character:GetPrimaryPart()
	local SelectedSpawn = SpawnTables[math.random(1, #SpawnTables)]
	HumanoidRootPart.CFrame = SelectedSpawn.CFrame
	table.remove(SpawnTables, SelectedSpawn)
end

--Checks if the player dies and runs the function when the player spawns
LocalPlayer.CharacterAdded:Connect(function(Character)
	local Humanoid = Character.Humanoid
	Humanoid.Died:Connect(function()
		task.wait(Players.RespawnTime)
		tpToSpawn(Character)
	end)
end)```
2 Likes

What would GetPrimaryPart mean?

1 Like

Models in ROBLOX have something called a PrimaryPart, that PrimaryPart is basically the root of the model. GetPrimaryPart just returns that Part.

1 Like

It’s the same as Character.PrimaryPart. The PrimaryPart of the character is always the HumanoidRootPart. The HumanoidRootPart is just an invisible part of the character, that acts as the “core”. It determines stuff like the players position ect.

1 Like

It’s like Character.HumanoidRootPart though for this function it makes use so you don’t need to know the name “HumanoidRootPart” it automates the directory of the MainPart of a model
image_2023-06-16_155120156

1 Like

player and character seem to be having a orange line underneath it, could this be from me not referencing it or what could it be?

image

2 Likes

You should fix this to:

local AvailableSpawnPoints  = SpawnPoints:GetChildren()
if not AvailableSpawnPoints or not AvailableSpawnPoints[1] then
   warn("no spawnpoints available")
end

Also instead of removing the spawnpoints from the array, I suggest to use the player’s index as a way to make them pick the spawnpoints:

local totalSpawns = #AvailableSpawnPoints
for i, player in pairs(plrs) do
	local character = player and player.Character
	if character then
		local humRP = character:FindFirstChild("HumanoidRootPart")
		if humRP then 
			humRP.CFrame = AvailableSpawnPoints[((i-1)%totalSpawns)+1].CFrame + Vector3.new(0,10,0)
		else
			warn(player.Name .. " does not have a root."
		end
	end
end