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

Why is my absolute deviation script not functioning due to a boolean error?

Hello. I have made a script to detect the absolute deviation of the character's torso position to another given point. The variables and actual mathematical statement are working, but it's the if-then that doesn't allow it. I'll show you what I mean:

local player = game.Players.LocalPlayer
local char = player.Character
local givenPoint = Vector3.new(math.random(-100, 100), math.random(-100, 100), math.random(-100, 100))
local cpos = char.Torso.Position
local x = cpos.x
local xabsdev = math.abs(math.abs(x) - math.abs(givenPoint.x))
print(xabsdev) --this right here works as a number value
if 0 <= xabsdev <= 1 then
	print('nfdlsa')
end

When I run this script, the print works perfectly, but it's the absolute deviation if-then statement that returns the error: attempt to compare boolean with number. How is 0 <= xabsdev <= 1 comparing a bool to a number? If this seems dumb please don't leave hateful comments, I have been trying to solve this for an hour. Thanks for reading and responding.

1 answer

✓ Accepted answer

This is happening because Lua first tries to compare 0 <= xabsdev, which evaluates to either true or false and then it tries to compare true <= 1, which makes no sense to it.

You can check whether your variable is within a certain range using two comparisons and the and logical operator, like so:

if 0 <= xabsdev and xabsdev <= 1 then
	-- Do something
end

2 comments

0

Thanks! Worked like a charm

0

'Glad I could help. :)

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