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

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

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

1 comment

0

Is "Goblet" a folder or some sort that holds all the globlets in your inventory?

3 answers

✓ Accepted answer

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

4 comments

1

That should work

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)

0

But wouldnt that remove the money the player previously have?

0

Oh wait the correct one is plrmoney = plrmoney + goblets*5, ill fix it

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.

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