Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to move a player backwards by a fixed amount?

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)

2 comments

0

Instance:FindFirstChild(string name) not Instance:FindFirstChild:(string name). Also, is "move back" here walking back or teleporting back?

0

oh i know abt the findfirstchild thingy but i mean teleport back

1 answer

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:

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.

2 comments

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.