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

attempt to compare userdata with number, Fix?

I made a module that will check if you have enough coins to buy the provided item, but it always says the error: "attempt to compare userdata with number" Here is the module script

module["PurchaseItem"] = function(item, cost, player)
	local coin = player.leaderstats.Coins
	
	if coin.Value >= cost then
		coin.Value = coin.Value - cost
		
		local Item = game.ReplicatedStorage[item]:Clone()
		Item.Parent = player.PlayerGui.Menu.Inventory.Frame
	end
	
end

And here is the script calling the function

Item.Common.Buy.MouseButton1Click:Connect(function()
	Module["PurchaseItem"](Item.Common.Name, Item.Common.Price, script.Parent.Parent.Parent.Parent.Parent)
end)

3 answers

✓ Accepted answer

Are you sure the cost variable isn't an IntValue or Instance of any kind? If that's the case, you should use .Value like you did with the coins and it should work

#Potential Problems

  1. On line 4 you tried Comparing a numbe with A value Instance.
  2. On line 5 you also tried Subtracting a number with a Value Instance.

#Solutions On your second script, The second argument of the function call should be Item.Common.Price.Value

#What are Userdatas?

Userdata is a Lua datatype that allows C to Add custom behavior to Lua using the Lua API. All Roblox objects are Userdatas which allows custom behavior to Roblox Classes, such as Read-Only values, You can only Read/Write to Existing values and the Table that holds the construtors such as Instance are true Read-Only tables.

I'm not sure about whats wrong with your script since your not showing everything but. try tonumber(cost)

and instead of script.Parent.Parent.Parent.Parent.Parent trying using game:GetService("Players").LocalPlayer if your doing this on local script.

also, double check your arguments on this line.

Module["PurchaseItem"](Item.Common.Name, Item.Common.Price, script.Parent.Parent.Parent.Parent.Parent)

and lastly double check your variable for coins too.

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