Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 8 years ago

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

Log in to vote
1
Answered by
Link150 1355 Badge of Merit Moderation Voter
8 years ago

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
0
Thanks! Worked like a charm yoman1776 85 — 8y
0
'Glad I could help. :) Link150 1355 — 8y
Ad

Answer this question