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

How to make a player face a certain direction when they teleport?

Asked by
snarns 25
6 years ago

When a player teleports, I would like for them to face a toward a fixed spot, but I'm not sure how. Is there a way to do this?

This script is to teleport the player to an area when they click a button on a gui. If the solution could be implemented within this script that would be great!

Player = game.Players.LocalPlayer
SpawnTo = game.Workspace.SpawnTele
TeleportButton = script.Parent.TeleportButton
HasPressedTeleport = false


function TeleportPlayer()
    if HasPressedTeleport == false then
        Player.Character:MoveTo(SpawnTo.Position)
        wait(0.1)

    end
end

TeleportButton.MouseButton1Down:connect(TeleportPlayer)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Using MoveTo isn't the way to do this, all parts will move with the hrp, so just rotate/move the hrp to teleport a player

(HRP = HumanoidRootPart)

For this, use CFrame

Btw, use remote events, don't teleport client side

Player = game.Players.LocalPlayer
SpawnTo = game.Workspace.SpawnTele
TeleportButton = script.Parent.TeleportButton
HasPressedTeleport = false


function TeleportPlayer()
    if HasPressedTeleport == false then
        local char = Player.Character

    if char then -- Might not be loaded
        local HRP = char:WaitForChild('HumanoidRootPart')
        HRP.CFrame = SpawnTo.CFrame -- You can rotate 'spawnto' to change the players rotation or use cframe angles
    end
    end
end

TeleportButton.MouseButton1Down:connect(TeleportPlayer)

If you have any question, or I made a typo/mistake, just ask :)

So as an edit for your question, the best way to do it is to set the camera quickly to attach, then back to custom

Player = game.Players.LocalPlayer
SpawnTo = game.Workspace.SpawnTele
TeleportButton = script.Parent.TeleportButton
HasPressedTeleport = false
local Cam = workspace.CurrentCamera


function TeleportPlayer()
    if HasPressedTeleport == false then
        local char = Player.Character

    if char then -- Might not be loaded
        local HRP = char:WaitForChild('HumanoidRootPart')
        HRP.CFrame = SpawnTo.CFrame -- You can rotate 'spawnto' to change the players rotation or use cframe angles

        repeat
            wait()
            Cam.CameraType = Enum.CameraType.Attach
        until Cam.CameraType == Enum.CameraType.Attach

        repeat
            wait()
            Cam.CameraType = Enum.CameraType.Custom
        until Cam.CameraType == Enum.CameraType.Custom
    end     
    end
end

TeleportButton.MouseButton1Down:connect(TeleportPlayer)
0
Thank you for the speedy response! I'm pretty new to a lot of this so I'm unsure what you mean on line 13, and I don't know what to add there. snarns 25 — 6y
0
CFrame (CoordinatedFrame) is just a Player's position and Rotation combined, The CFrame is more accurate than Vector3 (X,Y,Z only) because Vector3 mostly teleports you on top of things and doesn't has a rotation in it, by setting your HRPs CFrame to the part's CFrame you set your rotation as the rotation of the part so you can choose how you're rotated on spawn, hope this helps lol User#20388 0 — 6y
0
You don't have to add anything, just rotate the spawnpart to change the player's rotation :) User#20388 0 — 6y
0
Ah I understand! Sorry for being so clueless haha. So the character faces the correct way, but the camera doesn't rotate with them. Would this be an easy fix? I'm so sorry for all the questions ;o; snarns 25 — 6y
View all comments (6 more)
0
That would be hard to do, read this --> http://wiki.roblox.com/index.php?title=API:Class/Camera (btw, please accept my answer :)) User#20388 0 — 6y
0
Aw, okay. Thanks for your time. snarns 25 — 6y
0
I edited the script, now it should rotate with you :) User#20388 0 — 6y
0
(It works by setting the camera quickly to attached and then back to custom) User#20388 0 — 6y
0
Thank you so much! It works! snarns 25 — 6y
0
Np :) User#20388 0 — 6y
Ad

Answer this question