Body force not moving player

Hi devs I want to make a script where a player moves in the opposite direction they are facing with a bodyforce there are no errors but I do not move when I activate the code

local camera = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local speed = 10
local char = plr.Character
local myHRP = char:WaitForChild("HumanoidRootPart")
local Tool = plr.Backpack:WaitForChild("SuperShotgun")
local bodyForce = Instance.new("BodyForce")
Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		print("running")
		local postpos = myHRP.Position +((myHRP.Position - camera.CFrame.p).unit * speed)
		bodyForce.Parent = myHRP
		bodyForce.Force = bodyForce.Parent.CFrame:vectorToWorldSpace(postpos)--line13
		print(postpos)
		wait(7)
		bodyForce:Destroy()
		bodyForce = Instance.new("BodyForce")
	end)
end)

body force dont work in Local Script but it can work if you give them network ownership from the server.
it is explained in the answer of this topic

2 Likes

thanks ill look at it ill keep the topic open in case

1 Like

after running the code trice this error comes up

Players.hellothere3681.PlayerGui.LocalScript:13: attempt to index nil with 'CFrame'  -  Client - LocalScript:13
1 Like

try this i think

 bodyForce.Force =  myHRP.CFrame:VectorToWorldSpace(postpos)

i looked inside the player and found that there was no bodyforce in the HRP

That mean it didn’t create a body force that’s why the error came up.did you know why did the bodyforce don’t create?

I don’t know if this’ll work, but maybe try creating the body force inside the function?
This may solve the missing body-force issue.

Network ownership has not been depreciated, I don’t know where you are getting that information from, but no it’s not depreciated.

2 Likes

Players already have network ownership over their character so this isn’t relevant. It’s perfectly fine to do what OP is doing in this case.


I ended up messing around with your script a bit and got it working. I will say though that the player has to jump for the force to work well sometimes. I guess the legs/feet have a lot of friction by default?

local camera = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local speed = 5000
local char = plr.Character or plr.CharacterAdded:Wait()
local myHRP = char:WaitForChild("HumanoidRootPart")
local Tool = script.Parent

local bodyForce = Instance.new("BodyForce")

Tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		print("running")
		local camCFr = camera.CFrame
		bodyForce.Force = camCFr.Position + (camCFr.LookVector * -speed)
		bodyForce.Parent = myHRP
		wait(1)
		bodyForce.Parent = nil
	end)
end)

Note I did change the Tool, speed, char, and the delay for getting rid of the BodyForce. Feel free to make them what you want though.

2 Likes

thanks this really helped i hope you have a nice day:)

1 Like