Apply a force to the player

I made a shotgun but i want it to bump the player in the opposite direction of where you’re looking. (for a shotgun jump)
I have everything done EXEPT the code that actually applies the force to the player. Iv tried everything i can think of and none of it has worked. Please help.

5 Likes

have you tried using body movers?

1 Like

iv tried every way i know how. this is why I’m so confused.

i think the best way to do it, is to apply for example; a bodyForce to the player, and set its force to the inverse of the shotgun’s barrel CFrame.LookVector.

I did something similar to add “recoil” to my tank and this is the result:

1 Like

If this the result youre looking for, then you can take the code and play around with it until you get the desired effect

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:connect(function()
	local PlayerOrientation = Player.Character.HumanoidRootPart.CFrame.LookVector --this will get the orientation that the player is looking at in the world space
	local VelocityModifier = 50 --the higher the number, the higher the velocity 
	local MaxForce = Vector3.new(50000,50000,50000) --higher numbers = more force applied to the player

	
	local bodyVelocity= Instance.new('BodyVelocity')
	bodyVelocity.Parent = Player.Character.HumanoidRootPart
	bodyVelocity.MaxForce = MaxForce
	bodyVelocity.P = math.huge -- this makes the bodythrusts P value infinite
	bodyVelocity.Velocity = Vector3.new(-PlayerOrientation.X*VelocityModifier,-PlayerOrientation.Y*VelocityModifier,-PlayerOrientation.Z*VelocityModifier)

	game.Debris:AddItem(bodythrust,0.1) --you can change the time to your liking, with the longer the wait, the longer the effect lasts
end)

3 Likes

I see your applying the force in a local script? Does that cause problems? Every time iv seen somebody apply a force they say to do it in a server script. from what iv seen.

1 Like

you can replicate the effect through the server using Remote Events, but if you have a high latency player, then the effect wont load on the client right away.

my work around this, to accommodate high latency players is that rather than replicating the effect to the server, i replicate the effect to each client.

my poorly made graph explains the process i use to make sure high latency players dont experience lag

this way, the effect is shown to every player, and in the case where the player who shot the gun has a high latency (I consider >75ms high), the client who shot the gun will have the effect immediately (which is the most important), and the other players will see the effect a bit later.

when making a multiplayer game, you want to make sure that the player stays engaged. If the player is shooting a gun, but it doesnt fire right away (lag) then they will probably not play or find the game as fun.

6 Likes

Hey that makes sense! Ill try your method.

I highly recommend you use BasePart | Roblox Creator Documentation on the HumanoidRootPart of the character. Then all you have to do is set the velocity to something like this:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Humanoid.RootPart -- a to get the root part

local forceMultiplier = 10 -- you'll need to mess with this
local jumpForceMultiplier = 50 -- since you mentioned a shotgun jump, you can use the value for multiplying the force the player is in the air

--When firing the gun:
Mouse.Button1Down:Connect(function()
	local directionVector = (HumanoidRootPart.Position - Mouse.Hit.Position).Unit
	local multiplier = if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then jumpForceMultiplier else forceMultiplier
	
	HumanoidRootPart:ApplyImpulse(directionVector * multiplier * HumanoidRootPart.AssemblyMass)
end)
4 Likes

Why would you replicate the forces acting on the client? I already understand replication and it’s my understanding that because the client controls the physics for their player everything that happens to it is automatically replicated to other clients.

1 Like

now that you mention it, I think youre correct. Physics should be replicated to every client, even if its not called from the server, but hit detection and bullet creation in this case should be replicated as i explained , to prevent lag in high latency players

Movement of the character model replicates to the server, if you were exerting a BodyMover force onto a generic part in the workspace then it would be best to do it in a server script or through the use of RemoteEvents as suggested (as otherwise the force would not be replicated to other clients).

I tried that but it doesn’t work in a server script.

1 Like