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

attempt to compare userdata with number, Fix?

Asked by 4 years ago

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

Log in to vote
3
Answered by
bum5Br 97
4 years ago
Edited 4 years ago

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

Ad
Log in to vote
1
Answered by 4 years ago

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.

Log in to vote
0
Answered by
CjayPlyz 643 Moderation Voter
4 years ago
Edited 4 years ago

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.

Answer this question