Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why cannot this code teleport the player?

Asked by 4 years ago

Because of the nature of my procedurally generated map, the player spawns under it. The problem is that whatever I try I can't get to teleport the player and he always falls into the void

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        print(char.Humanoid.Torso)
        local hum = char:WaitForChild("Humanoid")
        hum.Torso.CFrame = CFrame.new(Vector3.new(0,50,0))
    end)
end)

I actually can't understand why this code doesn't work, as it's almost identical to everything else I found online. I tried using both the RootPart and the Torso and simply nothing happens.

3 answers

Log in to vote
1
Answered by 4 years ago

The way I usually do something like this is use model:SetPrimaryPartCFrame(). It is a lot more consistent than trying to teleport the player via any of their body parts. The revised code would look something like this:

    game.Players.PlayerAdded:Connect(function(player)
        player.CharacterAdded:Connect(function(char)
            print(char.Humanoid.Torso)
        if char then 
            car:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0, 50, 0)))
        end 
        end)
    end)

This should work. If you want an alternative way of doing this, you should consider using SpawnPoints. Also, make sure to continually check if the character is actually there to make sure your code does not error.

I hope this helps!

0
This didn't work either, and I don't know why, after the game loads map generation is over, so I'm not sure if it's lag or Studio acting up. At the end of the day I just made it so that a spawn is created. Thanks for the reply, I will now try to make a teleport GUI to see if it works then comment back. ComradeG1Z 31 — 4y
0
The GUI works. I don't know what caused that ( probably the endless death loop, my best guess ). Thank you for the code! ComradeG1Z 31 — 4y
0
No problem! I hate that sometimes you try so hard to make your code work and then lag or some other stupid issue comes up. I'm glad you figured it out! Sir_Ragealot 72 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

ok i fixed it, btw u cant teleport player with humanoid so u need to use humanoidrootpart

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        print(char.Humanoid.Torso)
        local hum = char:WaitForChild("HumanoidRootPart")
        hum.CFrame = CFrame.new(Vector3.new(0,50,0))
    end)
end)
0
This doesn't seem to work either, and I don't know why, even after the map is generated it doesn't work, so it can't be because of lag ComradeG1Z 31 — 4y
0
wat is ur script? local script or normal script, if its local script, it doesnt work Friskyman321 121 — 4y
Log in to vote
0
Answered by 4 years ago

Don't use the humanoid when teleporting players. Instead, replace line 5 with something like:

char.HumanoidRootPart.CFrame = CFrame.new(0, 50, 0)

Unless your output specifically tells you to use Vector3, you can just put in the 3 numbers in the CFrame. I hope this helps :).

Answer this question