Spawn system wont teleport the player to the spawn

Help so i maked a spawn system that works like this
the player has a value, if the value is the same as the spawn number it will teleport the player to the spawn

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		-- teleport player to check point
		local level = player.leaderstats.Stage
		local stages = workspace.Spawns
		
		for _,part in pairs(stages:GetChildren()) do
			if part.Name:match(level.Value) then
				
				player.Character.HumanoidRootPart.CFrame = part.CFrame * CFrame.new(0,1,0)
			end
		end
	
  end)
end)

Are the spawns named by numbers? “1,2,3,4…”?

try removing pairs from the script.

Try this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		-- teleport player to check point
		local level = player.leaderstats.Stage
		local stages = workspace.Spawns

		for _,part in pairs(stages:GetChildren()) do
			if part.Name:match(level.Value) then
				warn("spawn found") -- if this doesnt print names are wrong
				game:GetService("RunService").Heartbeat:Wait() -- if this line fixed it, issue was character was not ready to change its position
				player.Character.HumanoidRootPart.CFrame = part.CFrame * CFrame.new(0,1,0)
			end
		end

	end)
end)

Another thing, are you sure, the value inside leaderstats folder exist before trying to use it with this code?

Plus, you could do it like this instead:
(no need to iterate on the parts in folder)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		-- teleport player to check point
		local level = player.leaderstats.Stage
		local stages = workspace:WaitForChild("Spawns")

		game:GetService("RunService").Heartbeat:Wait()
		char:WaitForChild("HumanoidRootPart").CFrame = stages:FindFirstChild(level.Value).CFrame * CFrame.new(0,1,0)

	end)
end)
1 Like

amazing it worked i think it was because the character was not ready to change the position thank you so much

1 Like

Sounds great!. Be sure to check this approach, which doesnt need the for loop:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		-- teleport player to check point
		local level = player.leaderstats.Stage
		local stages = workspace:WaitForChild("Spawns")

		game:GetService("RunService").Heartbeat:Wait()
		if stages:FindFirstChild(level.Value) then
			char:WaitForChild("HumanoidRootPart").CFrame = stages:FindFirstChild(level.Value).CFrame * CFrame.new(0,1,0)	
		end

	end)
end)
1 Like

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