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

What is userdata value and how can I fix my script?

Asked by 4 years ago

I'm trying to make a salesman who will buy all the "Goblets" in your inventory but I'm getting the error attempt to get length of upvalue 'goblet'(a userdata value)

Heres my script:

local click = script.Parent.ClickDetector

local plrmoney
local goblet


click.MouseClick:Connect(function(player)
    goblet = player.Backpack:FindFirstChild("Goblet")
    plrmoney = player.Backpack.MONEY
    for i = 1,#goblet do
        plrmoney.Value = plrmoney.Value + 5
    end
end)

my error is

  18:41:46.564 - Workspace.Salesman.Script:10: attempt to get length of upvalue 'goblet' (a userdata value)

The script is a server script that is within an npc

0
Is "Goblet" a folder or some sort that holds all the globlets in your inventory? Azure_Kite 885 — 4y

3 answers

Log in to vote
1
Answered by
cegberry 432 Moderation Voter
4 years ago

What is a userdata?: An userdata value is a C object, usually made from the C side of Roblox's engine, in this case.

Why is it erroring?: You can't use the # operator on the userdata.

How to fix it?: You can do a loop, and do a name check, for goblets in the player's inventory.

Here is a example:

local backpack = game.Players.LocalPlayer.Backpack

local goblets = 0

for index,object in ipairs(backpack:GetChildren()) do -- We get the children of Backpack
    if object.Name == "Goblet" then -- Check if the object's name is "Goblet"
        goblets = goblets + 1 -- Add up one number to show that we found a goblet
    end
end

print("There are "..goblets.." goblets, in the backpack") -- Show how many goblets we have

goblets = 0 -- Reset value
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago
local click = script.Parent.ClickDetector

local plrmoney  = 0
local goblet = 0


click.MouseClick:Connect(function(player)
    items = player.Backpack:GetChildren() -- Get childrens of backpack
    plrmoney = player.Backpack.MONEY

--Count amount of goblets
   for i, v, ipairs (items) do
        if v.Name == "Goblet" then
        goblet  = goblet+1
        v:Destroy()
    end
  end

--give money
    plrmoney = plrmoney + 5*goblet
end)

wrote this on phone tell me if it work

1
That should work cegberry 432 — 4y
1
Although that seems to give twice the amount of player money and multiplies it by goblets, I don't think you want this behaviour? Use this instead: (plrmoney = plrmoney * goblet) cegberry 432 — 4y
0
But wouldnt that remove the money the player previously have? Azure_Kite 885 — 4y
0
Oh wait the correct one is plrmoney = plrmoney + goblets*5, ill fix it Azure_Kite 885 — 4y
Log in to vote
0
Answered by 4 years ago
local click = script.Parent.ClickDetector

local plrmoney
local inv

click.MouseClick:Connect(function(player)
       inv = player.Backpack:GetChildren()
       plrmoney = player.Backpack.MONEY
       for i,v in pairs(inv)
            if v.Name == "Goblet" then
            plrmoney.Value = plrmoney.Value + 5
            v:Destroy()
end
end)

Player gets money and the item gets destroyed.

Try this, if any more errors, reply.

Answer this question