Raycast gun ONLY deals damage when a humanoid's health is exactly the same as its damage

Hello There!

As the title says, I’m making a raycast gun which works for the most part but has a weird glitch in it, that being that it will not damage anyone who doesn’t have the same health as its damage. (For example, the gun will deal 100 damage. Anyone who doesn’t have exactly 100 Health won’t be killed).

This is the snippet of the code.

local raycast = workspace:Raycast(GunHandle.Position, (MousePOS - GunHandle.Position).Unit * 200)
		local hitbox = raycast.Instance
		if hitbox then
			local char = hitbox.Parent
			local Humanoid = char:FindFirstChild("Humanoid")
			if Humanoid and char ~= Plr.Character and Humanoid.Health >= 1 then
				Humanoid:TakeDamage(384)
				
				if Humanoid.Health <= 1 then
					local BodyVelocity = Instance.new("BodyVelocity",char.PrimaryPart)
					BodyVelocity.MaxForce = Vector3.new(1, 1, 1) * math.huge

					local Dir = (char.PrimaryPart.CFrame.Position - Plr.Character.PrimaryPart.CFrame.Position).Unit
					BodyVelocity.Velocity = (Dir + Vector3.new(0, 3, 0)).Unit * 250
					game:GetService("Debris"):AddItem(BodyVelocity,.025)
				end
			end
		end
4 Likes

maybe try

Humanoid.Health = Humanoid.Health-384
1 Like

Didn’t work sadly, plus I don’t want the gun to pierce through forcefields.

1 Like

Does

local raycast = workspace:Raycast(GunHandle.Position, (MousePOS - GunHandle.Position).Unit * 200)
local hitbox = raycast.Instance

if hitbox then
    local char = hitbox.Parent
    local Humanoid = char:FindFirstChild("Humanoid")

    if Humanoid and char ~= Plr.Character and Humanoid.Health > 0 then
        Humanoid:TakeDamage(384)

        if Humanoid.Health <= 0 then
            local BodyVelocity = Instance.new("BodyVelocity", char.PrimaryPart)
            BodyVelocity.MaxForce = Vector3.new(1, 1, 1) * math.huge

            local Dir = (char.PrimaryPart.CFrame.Position - Plr.Character.PrimaryPart.CFrame.Position).Unit
            BodyVelocity.Velocity = (Dir + Vector3.new(0, 3, 0)).Unit * 250
            game:GetService("Debris"):AddItem(BodyVelocity, 0.025)
        end
    end
end

work at all? I just changed the if statement. If it doesn’t, try to print out the boolean of every part of the if statement before the damaging, and try to detect if the script even reaches the :Damage() part.

1 Like

I’ve already somehow found the issue, apparently, a humanoid with a huge amount of health like 10000000000 makes it invincible against the gun for some reason? Idk, but I’ll try this later, thanks!

10000000000 goes over the computable integer limit haha, so it just re-registers as “inf” or “infinity”. That makes a lot more sense!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.