Attempt to compare boolean and number

Hello, I’m making a script for a gun but the gun is not working.
Error in console: Attempt to compare boolean and number

LocalScript:

local player = game.Players.LocalPlayer

local CharacterPlayer = player.Character or player.CharacterAdded:Wait()

local Mouse = player:GetMouse()

local Ammo = 5

local AmmoLeft = 5

local MaxDistance = 70

local Equipped = false

local Reloading = false

script.Parent.Equipped:Connect(function()

Equipped = true

game.ReplicatedStorage.Ammo:Fire(player, Ammo)

Mouse.Icon = "rbxasset://textures/MouseLockedCursor.png"

end)

script.Parent.Unequipped:Connect(function()

Equipped = false

game.ReplicatedStorage.AmmoUnequipped:Fire(player)

Mouse.Icon = "rbxasset://textures/ArrowFarCursor.png"

end)

Mouse.Button1Down:Connect(function()

local Target = Mouse.Target

local HumanoidPlayer = CharacterPlayer:FindFirstChild("Humanoid")

if Target.Parent:FindFirstChild("Humanoid") and AmmoLeft >= 1 and Equipped and not Reloading 
and (Target.Parent.HumanoidRootPart.Position - 
CharacterPlayer.HumanoidRootPart.Position).magnitude <= MaxDistance then

local Character = Target.Parent

local Humanoid = Character:FindFirstChild("Humanoid")

if not Humanoid.Health <= 0 and not HumanoidPlayer.Health <= 0 then

AmmoLeft = AmmoLeft -1

script.Parent.Handle.FireSound:Play()

game.ReplicatedStorage.LessAmmo:Fire(player)

script.Parent.TakeDamageEvent:FireServer(Character)

elseif not Target.Parent:FindFirstChild("Humanoid") and AmmoLeft >= 1 and Equipped and not 
HumanoidPlayer.Health <= 0 and not Reloading then

AmmoLeft = AmmoLeft -1

script.Parent.Handle.FireSound:Play()

game.ReplicatedStorage.LessAmmo:Fire(player)

elseif AmmoLeft == 0 and Equipped and not HumanoidPlayer.Health <= 0 and not Reloading then

Reloading = true

game.ReplicatedStorage.Reload:Fire(player)

script.Parent.Handle.Reload:Play()

wait(3)

AmmoLeft = Ammo

Reloading = false

end

end

end)

It seems that the error is in that line:

if not Humanoid.Health <= 0 and not HumanoidPlayer.Health <= 0 then

What if you try printing both values (Humanoid.Health and HumanoidPlayer.Health) before that line?

EDIT: You cannot use “not”. Remove, reverse the condition and try again!

It says: “100” that is the current health of both players.

NOT can’t be used when comparing a string with a number, at least not in this form.

This works:

local a = 5
if a <= 5 then print("Works") end

But the following doesn’t:

local a = 5
if not a <= 4 then print("Works") end
1 Like

Hmm try using brackets, might be the order the logic gets applied to.

if not (Humanoid.Health <= 0) and not (HumanoidPlayer.Health <= 0) then
1 Like

Adding on to what @dthecoolest and @DevTestDummy said, the compiler would read this as:

if (not Humanoid.Health) <= 0 and (not HumanoidPlayer.Health) <= 0 then
-- the places where the parenthesis are is what the compiler does first
-- they returns booleans and it attempts to continue but as the error says, "Attempt to compare boolean and number"
1 Like