Hit-scan Gun Errors

I’ve been trying to make a simple hit-scan gun using raycasting and I’ve stumbled into a big error.

Error:
Whenever the mouse is hovering on the character I wish to shoot at, it does no damage, despite the bullet being directly at the mouse’s location. However, when I move it at an angle, it works, but to a degree.
Another error the gun seems to have is when the bullet has to look for the humanoid of the character. It would eventually break the gun and return the error in the output which I put in the link below.

Screenshot of the error: https://gyazo.com/d2981eba53beb52ada3da72b8b602195

I’ve tried going on youtube to look at different methods and each attempt yields the same error, a nil value.
–Server

local damage = 13

game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(Player, MuzPos, BigPos,tool,target)
	local raycast = Ray.new(BigPos, (MuzPos - BigPos).unit * 1500)
	local Part,Position = game.Workspace:FindPartOnRay(raycast, Player.Character, true, false)
	--Skipped out on the less important stuff in the script. The bullet was already defined before as an Instance.new
	local dist = (MuzPos - BigPos).magnitude
	bullet.CFrame = CFrame.new(BigPos,Position) * CFrame.new(0,0,-dist/2)
	bullet.Size = Vector3.new(0.1,0.1,dist)
	if Part then
		local hum1 = Part.Parent:FindFirstChild("Humanoid")
		local hum2 = Part.Parent.Parent:FindFirstChild("Humanoid")
		local hum3 = Part.Parent.Parent.Parent:FindFirstChild("Humanoid")
		
		if hum1 then
			hum1:TakeDamage(damage)
		elseif hum2 then
			hum2:TakeDamage(damage)
		elseif hum3 then
			hum3:TakeDamage(damage)
		end
	end
	game:GetService("Debris"):AddItem(bullet,0.05)
end)

-LocalScript

game.ReplicatedStorage.Damage:FireServer(tool.SmokePart.Position,mouse.Hit.Position,tool,mouse.Target)
--Everything else is just basic stuff about the gun like ammo and other jazz.

Like I previously stated, it is a simple hit-scan gun.

1 Like

Is your raycast ignoring the shooter’s character and gun? I don’t see so from the code. Try FindPartOnRayWithIgnoreList and ignore their character Model.

Whoooops. Seems like I read it wrong. Ignore this… I see the second argument to FindPartOnRay now.

1 Like

try recursively checking hum = hit:FindFirstChild("Humanoid")
and doing hit = hit.Parent
until hit.Parent is nil or the workspace (or hum has been found)
and then if hum then
Also what is muzpos and bigpos, because if muzpos is the muzzle then your direction is wrong.
Its (To - From).Unit

1 Like

For OP - to explain (To - From), it’s To, but relative to From. Basically what you have to add to From in order to get To.

1 Like

Ye thats the point
To - From = Direction * Magnitude
Direction = (To - From).Unit ((/ Magnitude))
To = From + Direction * Magnitude ((To - From))
because it is an offset relative to From

1 Like

That math is really confusing…

1 Like

Hey try doing this :smiley:

local damage = 13
local bullets = workspace:FindFirstChild("Bullets") or Instance.new("Folder")

game.ReplicatedStorage.Damage.OnServerEvent:Connect(function(Player, MuzPos, BigPos,tool,target)
	local raycast = Ray.new(MuzPos, (BigPos - MuzPos).unit * 1500)
	local Part,Position = game.Workspace:FindPartOnRayWithIgnoreList(raycast, {Player.Character,bullets}, true, false)
	--Skipped out on the less important stuff in the script. The bullet was already defined before as an Instance.new
	local dist = (MuzPos - BigPos).magnitude
	bullet.CFrame = CFrame.new(BigPos,Position) * CFrame.new(0,0,-dist/2)
    bullet.Size = Vector3.new(0.1,0.1,dist)
    bullet.Parent = bullets
    if Part then
        local parent = Part.Parent
        local humanoid = parent and parent:FindFirstChild("Humanoid") or parent and parent.Parent and parent.Parent:FindFirstChild("Humanoid")
        
        if not humanoid then 
         warn'no humanoid' 
         else
        humanoid:TakeDamage(damage)
        end

	end
	game:GetService("Debris"):AddItem(bullet,0.05)
end)

Well the first time I used hit scan I liked it but then I used Ray Cast which I prefer more because you can do a lot more. But if you want to keep using hit scan for a gun consider watching this video.
Link: https://youtu.be/rcwWOPq1sJg

If you want to try out ray cast which can be more reliable at points of time here is the link.
Link: https://youtu.be/OWlzX334grI