Hi, when messing around with some scripts, I found a weird error in my code. When the events fires, it doesn’t teleport the player to location. The script does not show any errors.
game.Players.PlayerAdded:Connect(function()
plr.CharacterAdded:Connect(function(character)
local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if HumanoidRootPart then
HumanoidRootPart.CFrame = CFrame.new(Vector3.new(1, 15, 1))
end
end)
end)
The expected result is the character gets teleported to the cframe showed. If anyone knows why this happens please let me know, thanks.
This script does work as intended but you need to add a wait in either in the if statement or before.
The reason for this is because the character loads in, it teleports itself back to (0,0,0) or whatever the spawn plate is at. If you have a tiny delay like wait(0.1) the character loads in and then is teleported after it is spawned.
if HumanoidRootPart then
wait(0.1)
HumanoidRootPart.CFrame = CFrame.new(Vector3.new(1, 15, 1))
end
It’s not working because you are trying to find the HumanoidRootPart before it is created. Replace FindFirstChild with WaitForChild or wait until the character is a descendant of the workspace and then create the variable.
if not character:IsDescendantOf(workspace) then
repeat
task.wait()
until character:IsDescendantOf(workspace)
end
local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")