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

How to move a player backwards by a fixed amount?

Asked by
Cjjdawg -9
5 years ago

hi i was wondering if anyone could help me how to move a player back by a certain amount after touching a part.

this is what i have and it is not working

Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild:("Humanoid") then
        local character = hit.Parent
        character:MoveTo(character.PrimaryPart.Position+Vector3.new(0,0,5)
    end
end)
0
Instance:FindFirstChild(string name) not Instance:FindFirstChild:(string name). Also, is "move back" here walking back or teleporting back? Ankur_007 290 — 5y
0
oh i know abt the findfirstchild thingy but i mean teleport back Cjjdawg -9 — 5y

1 answer

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

A CFrame value has a LookVector property that returns the front face of the CFrame value.

You can use this property and multiply this by a number (in studs, whether it be negative or positive), and then increase it by a Vector3 in order to offset the CFrame. It's pretty cool.

Here's an example:

lua workspace.xd.CFrame = CFrame.new(workspace.xd.CFrame.LookVector*-5 + workspace.xd.Position) * CFrame.Angles(workspace.xd.CFrame:toEulerAnglesXYZ()) --CFrame.Angles to keep the rotation of the part.

You can use this on the HumanoidRootPart to set a player back 5 studs.

Note that using the Position property on the a part breaks all joints related to the part and makes the part fall if not anchored. Do not use this on major body parts such as the head or torso, as this will eliminate the player.

0
the only issue that i have is that the character is keep moving back by a larger and larger amount each time they touch it how can i stop this Cjjdawg -9 — 5y
0
If you wish to stop this, then add a debounce. It's likely the .Touched function firing multiple times when someone touches it, therefore launching the character back. Fifkee 2017 — 5y
Ad

Answer this question