Help with creating a script that when a player goes a certain distance to the part, the player gets sent backwards

Hello, I want to create a script that when a player goes a certain distance to a part, the player gets pushed backwards.
I would like some tutorials or examples.
Thank you!

You could either use BodyMovers or TweenService, you’d also have to use the .Magnitude property of Vector3s to get the distance between the character and your desired part/position.

BodyMovers would be more dynamic but TweenService would probably do a better job as it moves the player to a specific part, regardless direction.

1 Like

Thanks! I will check the links out.

Also, I want the player to fly and get pushed backwards, for example, a strong wind pushing player. Do you have any examples for this? Thanks.

You could probably use one of the BodyMovers to push the player up as well as back.

1 Like

Ok, Thank you for this information!

Is there any way to put BodyMovers in Players?

Yep, put it into their Character’s HumanoidRootPart.

1 Like

Ok, Thanks. Sorry If I am asking too many questions; I’m not familiar to coding. :slight_smile:

I looked at the link, and the code that Roblox provided seems to not work.
Here is a picture of it:


Can you help me with this problem? Thanks!

Is this just raw code, not wrapped in a function?

I don’t know because i found this in BodyForce | Roblox Creator Documentation.

Could you send your full script, or was your full script in that photo?

That was my full script in the photo.

Gotcha.

You would have to wrap it in a function to check if the distance from the target is closer than a specified distance, if it is, apply the bodyforce to the HRP.

1 Like

Oh, ok, Thanks! Also I took a video about the system that i want :grinning:
The blue part is representing the player.
When player jumps:


When player walk towards it:

Also does it have to be in a LocalScript?

It could be a localscript.

Here’s something you could work with:

local runService = game:GetService('RunService')
local players = game:GetService('Players')

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')

local maxDistance = 10
local distanceToKeepFrom = Vector3.new(0,0,0)

runService.RenderStepped:Connect(function()
    local distance = (humanoidRootPart.Position - distanceToKeepFrom).Magnitude
    if distance <= maxDistance then
    -- You'd apply the force here
    end
end)
1 Like

Thanks! I will test it out and let you know how it worked :grinning:

Do I have to put the force as (0, 0, 0)? or is there a different way to write the force? Sorry, I’m really new with coding. :sweat_smile: