This gun is unreliable, any simple solutions?

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

script.Parent.Activated:Connect(function()
	local Ammo = script.Parent.Parent:FindFirstChild("Ammo")
	if Ammo.Value > 0 then 
		script.Parent.LoseAmmo:FireServer(Ammo)
		for count = 1, 5 do
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.DealDamage:FireServer(Mouse.Target.Parent)
			end
		end
	end
end)

This localscript is for a gun. It works completely fine, except it has a chance to not hit the character despite the accuracy being flawless. I tried to counter this by checking the mouse’s target multiple times, but the same issue happens and it isn’t very practical. Are there any solutions to this problem?

So can you tell me how you check if it fails because I don’t really have much to go off besides that this script works but it sometimes doesn’t hit the target.(it worked perfectly fine for me without missing) (Also try printing the target parent name to see what it thinks it’s hitting maybe it just thinks it’s touching something else)

Sorry if I was being unclear. The gun is supposed to deal 100 damage to the player. Sometimes when the mouse is directly on the player and the gun shoots, it still doesn’t kill them. I will print the target name and hopefully, it will help. Thanks for your help so far.

Thank you so much for this helpful idea. Turns out the gun was actually hitting the handle of the tool. No further help is needed.

Alright that’s good just filter the gun parts and your good glad I could help

Side Note

A little side note, exploiters can fire remote events from their client when ever they want, so adding this basically gives exploiters the ability to kill any player in the server.

I would recommend doing your ray casting on the server based on a Ray you send from a remote event.
Edit: Also sanity check the ray sent by the remote too (check if its coming from somewhere reasonable).

Agreed, this script is insecure.
Here is a quick hack script. You should try making these yourself whenever you involve RemoteEvents. Hack yourself first!

edit: You can put this script in the gun to test it. When it runs (when the character spawns in, or picks up the gun from the floor), the script will disable ammo loss and kill everything. It doesn’t matter where the script will run, really. A user-friendly exploit maker will probably even make a GUI button or hotkey to run that second function.

-- LocalScript

local tool = script.Parent -- Make this variable the gun tool!

-- Infinite ammo hack
-- Replaces the RemoteEvent with a dud that never tells the server you're losing ammo
tool.LoseAmmo:Destroy()
local fakeRemote = Instance.new("RemoteEvent", tool)
fakeRemote.Name = "LoseAmmo"

-- Kill everyone and everything hack
-- Deals damage to every Humanoid in Workspace
local DealDamage = tool.DealDamage
local targets = {}
for _,v in ipairs(workspace:GetDescendants()) do
	if v.Name == "Humanoid" and v:IsA("Humanoid") then
		table.insert(targets, v.Parent)
	end
end
for i = 1, 30 do
	for _,v in ipairs(targets) do
		DealDamage:FireServer(v)
	end
end