How do I move the character with body force

I’m trying to create an addon to my weapon script which allows the character to strafe left or right by pressing “q” or “e” but cannot figure out how to do body forces to make this.

    Mouse.KeyDown:connect(function(Key)
    	if Equipped == true and Down == false then
    		if string.lower(Key) == "q" then
    			local bodyForce = Instance.new("BodyForce")
    			local Left = Vector3.new(-100, 0, 0)
    			bodyForce.Parent = Character.HumanoidRootPart
    			bodyForce.Force = bodyForce.Parent.CFrame:vectorToWorldSpace(Left)
    		end
    	end
    end)

Could anyone could suggest or give any help on how to successfully create this, It would be much appreciated.

1 Like

I suggest you use a BodyVelocity instead of a BodyForce, as they apply a constant amount of force unlike body forces

2 Likes

as @kisty1 said use a BodyVelocity

If using BV doesn’t work, you could try doing something like this

Mouse.KeyDown:connect(function(Key)
    	if Equipped == true and Down == false then
    		if string.lower(Key) == "q" then
    	        local left = Vector3.new(-20,0,0)
                for i = 0, 1, .05 do
                        local CharPos = character:GetPrimaryPartCFrame()
                        character:SetPrimaryPartCFrame(CFrame.new(CharPos):vectorToWorldSpace(Left.X * i, Left.Y, Left.Z))
                end
    		end
    	end
    end)
1 Like

Your best option is to set the velocity of the HumanoidRootPart directly. This will also allow you to include velocity from gravity as part of the strafe.

Don’t set the CFrame of the HumanoidRootPart, because that won’t be physically simulated and the character will be able to move through obstacles.

2 Likes

Could you explain how to do this i’m not familiar with body forces at all to be honest.

1 Like

It’s not BodyForces, what @Shardwielder is trying to say is, under every basepart there’s a velocity property which you can change. It works like bodyVelocity except the bodyVelocity kind of lerps the movement. Just do

local left = Vector3.new(-100,0,0)
HumanoidRootPart.Velocity = left
1 Like