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

How to teleport behind players?

Asked by 4 years ago

I wanna create a move where it'll teleport a player behind the player facing the same direction and is always behind no matter what direction the other player is facing. However, I'm unsure of where to start as I have to keep orientation, and position to always be behind a certain amount.

0
Reposition the CFrame of the player you want to move to that of the player you want to move behind adding 5 or so stud to the Z axis Yishles 4 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

ah i see u r requesting an anime killer move: u asked the right website then.

roblox has these things called CFrames which can be only obtained via hacking real life. I was just kidding but that is basically how hard it is to visualize these things without a technical background. CFrames encapsulate both the orientation and position property of a part, basically rendering them useless in the context of scripting(except for position that still can be used to calculate the linear distance between vectors). To go around doing this u have to understand 2 things:

1 - if u set the CFrame of the player's basePart(the rootPart) to the CFrame of the enemy's rootPart, the player's rootPart will share the position and orientation of the enemy's rootPart(basically).

2 - u can combine CFrames(e.g if u do something like part.CFrame * CFrame.new(0, 0, 1) the result CFrame would be 1 unit in front of the part(no matter what direction)).

with these 2 in mind, we can make a script:

place in starterPack btw:

local root = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart")

game:GetService("UserInputService").InputBegan:Connect(function(input, process)
    if process then return end

    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        local nearestEnemy = nil
        local nearestRoot = math.huge -- this number can be seen as basically infinity

        for i, v in pairs(workspace:GetChildren()) do
            local plrRoot = v:FindFirstChild("HumanoidRootPart")

            if plrRoot and plrRoot ~= root then
                local mag = (plrRoot.Position - root.Position).Magnitude -- calculate linear distance between plrRoot and root

                if mag < 10 and mag < nearestRoot then -- if the distance mentioned earlier is more than 10 linear units or more than the current nearest root, we will not count it
                    nearestEnemy = plrRoot
                    nearestRoot = mag
                end
            end
        end

        if nearestEnemy then
            root.CFrame = nearestEnemy.CFrame * CFrame.new(0, 0, 3) -- make the root be 3 units behind the enemy's root, or in front of it in this case due to the fact the root(as a part)'s direction is opposite of the player's direction(as a guy)
        end
    end
end)
0
Thanks I'll try it right now ItsBankai 31 — 4y
Ad

Answer this question