It doesnt locate my Cframe to part even tho its right

It doesnt locate my Cframe to part even tho its right.


game.Players.PlayerAdded:Connect(function(Player)
	local Stage = Player:WaitForChild("leaderstats").Stages
	local Spawns = workspace:WaitForChild("Checkpoint")
	if Stage then
		for _, v in pairs(Spawns:GetChildren()) do
			if Stage.Value == v.Stage.Value then
				local Char = Player.Character or Player.CharacterAdded:Wait()
				local RootPart = Char and Char:FindFirstChild("HumanoidRootPart")
				if RootPart then
					
					RootPart.CFrame = v.CFrame
				end
			end
		end
	end
end)

Thank you.

You should really add breakpoints to see what isn’t working.
Also looking at the output is not a bad idea.

Try using Char:MoveTo instead?

Also this script loops through all the spawns and teleports the player to every one, not sure if that’s what you’re trying to do.

1 Like

The loop is going through all the spawn points and checking to see if Stage.Value == v.Stage.Value then to send the player to the correct spawn.

Also, @DylTheDeveloperX from what I understand this script only runs when a player first joins the game. Is that what you are trying to do, or do you want it to run every time a player spawns?

1 Like

Exactly thats what i’m trying to do.

Where is this script located, is it a localscript or a serverscript?

So what handles the player spawning after they die in the game?

Here.



game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		print("Spawned")
		local Stage = Player:WaitForChild("leaderstats").Stages
		local Spawns = workspace:WaitForChild("Checkpoint")
			if Stage then
				for _, v in pairs(Spawns:GetChildren()) do
					if Stage.Value == v.Stage.Value then
						local Char = Player.Character or Player.CharacterAdded:Wait()
						local RootPart = Char and Char:FindFirstChild("HumanoidRootPart")
					if RootPart then
						RootPart.CFrame = v.CFrame
					end
				end
			end
		end
	end)
end)

So why do you have 2 scripts doing the same thing?
The second script should handle both when a player joins the game, as well as when they die and spawn in the game.

And as @majdTRM asked you in 3 different posts:
Is this a Script or a LocalScript?
Try Char:MoveTo instead of CFrame.
Put different print(“whatever”) statements at points in the script using different terms so you can see in the Output window what is going on when the script runs (or doesn’t run).

2 Likes
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(Player)
	local Char = Player.Character or Player.CharacterAdded:Wait()
	local Leaderstats = Player:WaitForChild("leaderstats")
	local Stage = Leaderstats:WaitForChild("Stages")
	local Spawns = workspace:WaitForChild("Checkpoint")
	if Stage then
		for _, Spawner in ipairs(Spawns:GetChildren()) do
			if Stage.Value == Spawner.Stage.Value then
				Char:SetPrimaryPartCFrame(Spawner.CFrame)
			end
		end
	end
end)

This only moves the HumanoidRootPart, you need to use :PivotTo() on the character to move the character model.

I tried cleaning some parts of your code, it may have fixed the problem:

local Players = game:GetService("Players")

local Spawns = workspace:WaitForChild("Checkpoint")

function PlayerAdded(player)
	local function CharacterAdded(char)
		local leaderstats = player:WaitForChild("leaderstats", 5) 
		if not leaderstats then return end 
		local Stage = leaderstats:WaitForChild("Stages", 5)
		if not Stage then return end 
		
		local SpawnLocation = Spawns:GetChildren()[Stage.Value] 
		if SpawnLocation then 
			local cf = SpawnLocation.CFrame + Vector3.new(0, 5, 0)
			local root = char:WaitForChild("HumanoidRootPart", 5) 
			if root then 
				root.CFrame = cf
			end
		end
	end
	
	CharacterAdded(player.Character or player.CharacterAdded:Wait()) 
	player.CharacterAdded:Connect(CharacterAdded)
end

for i, player in pairs(Players:GetPlayers()) do 
	PlayerAdded(player)
end
Players.PlayerAddded:Connect(PlayerAdded)