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