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

Attempt to compare boolean with number?

Asked by 5 years ago

So I'm trying to make a script that when the Miles Driven [in leaderstats] is changed it fires a remote event to another script to check its value. If the value exceeds a limit it Ranks the player up. Everything seem to be working fine except the value in the if statement. It gives an error in the output: Attempt to compare boolean with number.

Here's the code:

local Event = game:GetService("ReplicatedStorage").RankUpServer

Event.OnServerEvent:Connect(function(player)
    print('Event connected')
    if player.leaderstats["Miles Driven"].Value >0<1 then
        player.leaderstats.Rank = "Permit"
    elseif player.leaderstats["Miles Driven"].Value >1<10 then
        player.leaderstats.Rank = "Novice"

    elseif player.leaderstats["Miles Driven"].Value >9<25 then
        player.leaderstats.Rank = "Intermediate"

    elseif player.leaderstats["Miles Driven"].Value >24<50 then
        player.leaderstats.Rank = "Advanced"

    elseif player.leaderstats["Miles Driven"].Value >49<51 then
        player.leaderstats.Rank = "Semi Pro"

    elseif player.leaderstats["Miles Driven"].Value >50 then
        player.leaderstats.Rank = "Pro"
    end
end)

Also the reason >49<51 looks like that I can't just do == 50 because it's suppose do be mile driven in a drag strip and it's 1/4 mile, so when the player races Miles driven get 0.4 added to it so I can't go with ==.

0
You really dont want to be using if statments like this it will end up in a mess. I would use a table that holds the rank and text. A linked list would probably be best. User#5423 17 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Relational Operators


The Reason for this is how relational operators work in lua, and in programming in general is that comparing two numbers with a relational operator returns a boolean, which is a true or a false

The particular reason why your code is erroring is the order in which the code is ran, which for relational operators is from left to right.

so, if i used your line 5 as an example:

if player.leaderstats["Miles Driven"].Value >0<1 then

the order at which it would run could be deduced mathmatically

(MilesDriven > 0)<1

true < 1

Therein lies the error


And Operator


The way we can prevent this would be to use an and operator in your code, which is primarily used to compare two booleans.

So, if we use that to fix you code like such:

Miles.Value >0 and Miles.Value<1

true and true

true


Application


With that said, here is the fixed code:

local Event = game:GetService("ReplicatedStorage").RankUpServer
local Miles = player.leaderstats["Miles Driven"]

Event.OnServerEvent:Connect(function(player)
    print('Event connected')
    if Miles.Value >0 and Miles.Value<1 then
        player.leaderstats.Rank = "Permit"
    elseif Miles.Value >=1 and Miles.Value<10 then
        player.leaderstats.Rank = "Novice"

    elseif Miles.Value >=10 and Miles.Value<25 then
        player.leaderstats.Rank = "Intermediate"

    elseif Miles.Value >=25 and Miles.Value<50 then
        player.leaderstats.Rank = "Advanced"

    elseif Miles.Value >=50 and Miles.Value<51 then
        player.leaderstats.Rank = "Semi Pro"

    elseif Miles.Value >=51  then
        player.leaderstats.Rank = "Pro"
    end
end)

I would also recommend using variables for the Miles and Rank stats

Hopefully this helped!

0
It did thank you! Babyseal1015 56 — 5y
0
no prob for the accept xf User#24403 69 — 5y
0
:D theking48989987 2147 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You cannot use the < > twice on the same variable at once. This is because the first time you define it, the function returns a bool (true or false, not a number) for the < > property, then checks it again against the next number.

You need to check the value twice in each line for this to work correctly

Also, rank is presumably a string value, so you will need to write .Value after each one or it will not run properly.

Revised Server Script

local Event = game:GetService("ReplicatedStorage"):WaitForChild("RankUpServer")

Event.OnServerEvent:Connect(function(player)
    print("Event Connected")
    local val = player:WaitForChild("leaderstats"):WaitForChild("Miles Driven").Value
    local rank = player:WaitForChild("leaderstats"):WaitForChild("Rank")
    if val > 0 and val < 1 then
        rank.Value = "Permit"
    elseif val >= 1 and val < 10 then
        rank.Value = "Novice"
    elseif val >= 10 and val < 25 then
        rank.Value = "Intermediate"
    elseif val >= 25 and val < 50 then
        rank.Value = "Advanced"
    elseif val >= 50 and val < 51 then
        rank.Value = "Semi Pro"
    elseif val >= 51 then
        rank.Value = "Pro"
    end
end)
0
Thank you. It works great. Babyseal1015 56 — 5y
0
that would be stuck in between Novice and Intermediate if Value is between 9 and 10, between Advanced and Intermediate if the miles is between 24 and 25 theking48989987 2147 — 5y

Answer this question