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

how do you compare "userdata" with numbers?

#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

3 comments

0

This should work, can you show the rest of the code?

1

If points is actually an object in the player's leaderstats or something, you will have to do points.Value

0

@hitonmymetatables alright ill show it

2 answers

✓ Accepted answer

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

So:

if points.Value >= 500 then

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).

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