Shooting Backwards

Shooting Backwards:
Hello. I wish to prevent backwards shooting, whenever the mouse is located behind the player, however I haven’t found the solutions for it. Here is what I wish to prevent basically:


The issue is that the code I am using isn’t really working, as it gives an error in the output. I’ve tried “messing” with the code, however nothing helped.

^Line 23

Server Script:

local SmallFireball = game.ReplicatedStorage.FireBall.SmallFireball
local Fireball = game.ServerStorage.Fireball
local Speed = 40
local Debounce = {}

SmallFireball.OnServerEvent:Connect(function(Player, mousePos, Handle)
	if table.find(Debounce, Player) then return end
	local HumanoidRootPart = Player.Character.Humanoid.RootPart
	local rightArm = Player.Character["Right Arm"]

	table.insert(Debounce, Player)
	local Distance = 200
	local Origin = Handle
	local Start = Origin.CFrame.p
	local Direction = (,, mousePos.Y, Player.Character.HumanoidRootPart.Position.Z))).lookVector
	local params =
	params.FilterDescendantsInstances = {Handle, rightArm, Player.Character}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastresult = workspace:Raycast(Start,Direction * Distance,params)
	local lookVector = (Player.Character.HumanoidRootPart).lookVector --Line 23
	local shootVector = Direction
	local dotProduct = Direction:Dot(lookVector)
	if dotProduct < 0 then return end
	local Fireball2 = Fireball:Clone()
    Fireball2.CFrame = rightArm.CFrame *, -(rightArm.Size.Y/2 + Fireball2.Size.Y + 1), 0)
	Fireball2.Parent = workspace
	local BodyVel ="BodyVelocity")
	BodyVel.Parent = Fireball2
	BodyVel.MaxForce =,1,0) * 30000
	BodyVel.Velocity = Direction * Speed
Event = Fireball2.Touched:Connect(function(Hit)
		local ParentHit = Hit.Parent
		local Humanoid = ParentHit:FindFirstChild("Humanoid")
        if not Humanoid then return end
		if ParentHit == Player.Character then return end
		Humanoid:TakeDamage(math.random(15, 30))
	if Fireball2 then
	delay(0.25, function()
		if not Player then return end
		table.remove(Debounce, table.find(Debounce, Player))

I’ve tried viewing a lot of topics regarding this issue including the following, however I have found no luck. If anyone is able to help me, it is appreciated.

Link: How to create Anti-Backward shooting precaution?

lookVector is part of CFrame.

Just do


And it’ll all be fine. :slight_smile:

It appears that it did solve the output issue, however now I am not able to shoot at all.

Welp, that’s not good. It’s likely that means your math is off. I’m not good at that, but I can try.

I tested it around and it seems that if I don’t click behind me, I am able to shoot in front of me. However if I do indeed shoot behind me, I can’t shoot in front of me at all and have to press the play button again to reset it.

I mean, at least you’re getting somewhere. I can’t seem to find the :Dot() function in the CFrame API. What is that?

Also remember, wait and delay are deprecated. The new methods are:

task.wait and task.delay. It’s just more efficient.

1 Like

If I am not mistaken, more information can be seen in the following post.

How to create Anti-Backward shooting precaution?

Thanks for the information as well. Didn’t really know.

1 Like

Not sure, I’m pretty bad at mathematical equations. Try messing around with the math to see what you can do.

I’ll see what I can do, to be honest. I might not be online for a bit, however if anyone wishes to help, they can.

You will have to compare the mouse position to the humanoid root part using CFrame.ToWorldSpace()

local MouseToCharacterPosition =  HumanoidRootPart.CFrame:PointToWorldSpace(Mouse.Hit.Position)
local Zaxis = MouseToCharacterPosition.Z

if Zaxis >= 0 then
    -- mouse is in front of character
    -- mouse is behind character

It might be the other way around since I did this from memory.

The solution in the post is probably a little better.


Yeah, you are right! That fixed my issue, but I still have one bug I need to fix. If I do click on the bottom or above my character, it still shoots, however I don’t like that.

So how are you able to detect if the mouse is above or below your character?

Yes actually.
You can actually reuse that logic.

I recommend doing what is said in the post where you should probably find the Dot Product instead. Since you can set a sort of “maximum angle of elevation” when firing a gun.

local CurrentPosition = HumanoidRootPart.Positiion
lcoal TargetPosition = Mouse.Hit.Position
local TargetVector = CurrentPosition - TargetPosition

local MAX_ANGLE_ELEVATION = 70 -- 
local DotProduct = HumanoidRootPart.CFrame.LookVector:Dot()
local DeviationAngle = math.cos(math.rad(DotProduct))
if not DeviationAngle > MAX_ANGLE_ELEVATION then
    -- Target is in the "angle"

One note: this formula makes a “cone shape” where the player can fire his gun. So, the players mouse needs to be in the cone for it to fire.

it would look something like this:

Good luck