Player not getting teleported

Hello Developers,

Tell me how to fix this, player is not getting teleported to his build plot

Script:

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder",game.Workspace.BuildBlocks)
    folder.Name = plr.Name
    wait(0.5)
    local part = Instance.new("Part",folder)
    part.Name = "PlayerStartingBlock"
    part.Size = Vector3.new(50,1,50)
    part.Anchored = true
    part.Position = Vector3.new(math.random(5,150),1,math.random(1,200))
    part.Material = Enum.Material.SmoothPlastic
    plr.CharacterAdded:Connect(function(char)
        char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame + Vector3.new(0,5,0)
    end)
end)

I tried keeping wait for child, but no use

1 Like

Make sure the game.Players.PlayerAdded function is almost at the top of the script before there are any waits.

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = player.Name
	folder.Parent = workspace.BuildBlocks
	
	local part = Instance.new("Part")
	part.Name = "PlayerStartingBlock"
	part.Size = Vector3.new(50, 1, 50)
	part.Anchored = true
	part.Position = Vector3.new(math.random(5, 150), 1, math.random(1, 200))
	part.Material = Enum.Material.SmoothPlastic
	part.Parent = folder
	
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local rootPart = humanoid.RootPart
		rootPart:PivotTo(part.CFrame + Vector3.new(0,5,0))
	end)
end)
1 Like
game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.CharacterAdded:Wait()
	local folder = Instance.new("Folder",game.Workspace.BuildBlocks)
	folder.Name = plr.Name
	wait(0.5)
	local part = Instance.new("Part",folder)
	part.Name = "PlayerStartingBlock"
	part.Size = Vector3.new(50,1,50)
	part.Anchored = true
	part.Position = Vector3.new(math.random(5,150),1,math.random(1,200))
	part.Material = Enum.Material.SmoothPlastic
	char:FindFirstChild("HumanoidRootPart").CFrame = part.CFrame + Vector3.new(0,5,0)
end)
1 Like

What is the error? and I kept the player added function on top but why you mentioned rootpart, could you tell please so in future scripts i can fix it

The error was the wait(0.5) in your script before you connected the .CharacterAdded event. This means that the player was added into the workspace before you connected the function to the event so it did not fire. You should avoid using wait functions except in loops due to this inconsistency.

RootPart is just my own way of getting the “HumanoidRootPart” since it is slightly more efficient. Same with :PivotTo(CFrame) which is recommended for moving anything and can move both parts and models.

1 Like

This is unrelated, but you should define the character this way:

local character = plr.Character or plr.CharacterAdded:Wait()

I’m pretty sure what you did might make it error sometimes, just saying from my experience.

1 Like

It probably errored because you had a :WaitForChild(), Event:Wait(), wait() or something somewhere. Any delays will likely cause the event to not fire when the character is added. Yes, your method works too but it will only fire once and not when the character respawns (assuming this is not under StarterCharacterScripts).

1 Like