Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to compare number and boolean error?

Hey there! I'm making a script where you can dash to the enemy your mouse is pointing at. I keep on getting an error.

Script (Local):

--[[ Values ]]--
local canDash = true

--[[ Math ]]--

--[[ Services ]]--
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")

--[[ Player Variables ]]--
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

--[[ Variables ]]--
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

--[[ Functions ]]--

--[[ Script ]]--
runService.RenderStepped:Connect(function()
	local mTarget = mouse.Target
	local humanoid = mTarget.Parent:FindFirstChild("Humanoid")
	local targetHumanoidRootPart = mTarget.Parent:FindFirstChild("HumanoidRootPart")
	
	if not targetHumanoidRootPart then return end
	
	local distance = (targetHumanoidRootPart.Position - humanoidRootPart.Position).Magnitude
	
	if mTarget.Parent.Name == player.Name then return end
	
	if humanoid then
		userInputService.InputBegan:Connect(function(input, gameProcessed)
			if input.KeyCode == Enum.KeyCode.E then
				
				local humanoid = mTarget.Parent:FindFirstChild("Humanoid")
				
				if canDash == true and humanoid.Health > 0 and humanoid and not distance > 40 then
					canDash = false
					humanoidRootPart.CFrame = targetHumanoidRootPart.CFrame
					wait(.3)
					canDash = true
				end
			end
		end)
	end
end)

Full Error:

17:24:15.855 Players.ColdFoxy07.Backpack.Katana.Dash:38: attempt to compare number and boolean - Client - Dash:38

I know the script is probably not very efficient, but I tried my best ^-^. Thanks for reading!

2 comments

0

uh whic part is it xd. what if you make and distance < 40 instead of not distance > 40

0

The error is on line 38, and changing distance to < instead of > did not work either.

2 answers

✓ Accepted answer

not is the highest priority operator and has higher priority than > so what it does is reverses the distance variable turning it into false and then only compares it to 40, you can use brackets to fix that:

if canDash and humanoid and humanoid.Health > 0 and not (distance > 40) then

The smarter way, however, would be using the <= operator which checks if distance is lower of equal to 40, this will remove need of the not operator:

if canDash and humanoid and humanoid.Health > 0 and distance <= 40 then

Notice that i reversed humanoid.Health with humanoid, this is because and operators go by order so if humanoid would be nil, it would still error because it would index it's Health first. It's also good to avoid long statements like this, you can do that using the return keyword:

if not humanoid or humanoid.Health <= 0 then
	return
end

if not canDash or distance < 40 then
	return
end

canDash = false
humanoidRootPart.CFrame = targetHumanoidRootPart.CFrame
wait(0.3)
canDash = true

1 comment

0

It worked! Thanks for replying!

The problem with this is that not takes precedence over almost every operator, which means that whatever operand not is adjacent to will be evaluated first. In this case, it would evaluate not distance first. Since distance is always going to be a truthy value, not inverts the state which evaluates it to false

So your whole expression simplifies to false > 40, which is, of course, what yields the error thrown. It is often emphasized that you should not "reinvent the wheel" as doing distance <= 40 is just what you need, but just in case you're wondering, you can still use the not operator with brackets so that whatever's in the brackets in evaluated first: not (distance > 40)

This discussion is preserved as it was. New answers, comments, and votes are closed.