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

how do you compare "userdata" with numbers?

Asked by 4 years ago
Edited 4 years ago

so..

i have this store and everything works fine until of course it says

LocalScript:11: attempt to compare number with userdata

yeah.. anyways they are both int values, but why can i not compare them?

edit: forgot to put the line that makes this error

if points >= 500 then

edit: full function here

wait(5.5)
Tool = false
local player = game.Players.LocalPlayer
points = player.leaderstats.Coins
local product = ".300 Carbine"
local prodlocation = game.ReplicatedStorage.NewWeaponStache.Carbines -- reminder to self: change this later

-----------------------------------------------------------------------------------------
function activate()
if points >= 500 then
    if Tool== false then 
    prodlocation:Clone().Parent = player.Backpack
    prodlocation:Clone().Parent = player.StarterGear
    points = points - 500
    Tool = true
    end

        elseif Tool == true then
        local tool = player.Backpack:GetChildren()

    if Tool and Tool ~= product then 
    Tool:Destroy()
    Tool = false
    prodlocation:Clone().Parent = player.Backpack
    prodlocation:Clone().Parent = player.StarterGear
    points = points - 500
    Tool = true
    end

    end
end
0
This should work, can you show the rest of the code? hitonmymetatables 50 — 4y
1
If points is actually an object in the player's leaderstats or something, you will have to do points.Value hitonmymetatables 50 — 4y
0
@hitonmymetatables alright ill show it speedyfox66 237 — 4y

2 answers

Log in to vote
1
Answered by
Hypgnosis 186
4 years ago

So points is an IntValue. You are referencing the object IntValue, instead of IntValue's value.

So:

if points.Value >= 500 then
Ad
Log in to vote
0
Answered by 4 years ago

You can't compare a number to a userdata because they're different data types. Simple as that.

However, there is an exception to this rule, and that exception is Enums. You can compare an Enum such as Material to a string and get away with it. You can't get away with it with any other userdata.

points is a userdata, specifically an IntValue. You can't compare a number to the IntValue itself, but you can compare a number to that Instance's Value property, because IntValue.Value is a number.

If you want to get the type of something, you can use the type() function, which returns the data type as a string. If you use it on both the IntValue and the number, you'll get these results:

print(type(points)) -- Prints "userdata"
print(type(500)) -- Prints "number"

It's common sense that you are unable to compare two pieces of data of different types. For example, comparing 500 to false doesn't make any sense because false is not a number (if you compare 500 directly) and 500 is not a boolean (if you compare false directly).

Answer this question