Trying to add item count to my inventory system

Hey everyone, I’m sure I’m making a novice mistake here but I need another set of eyes.

I’m trying to make a garbage can randomize between 3 items (or more!) and then once it picks one, it’ll add a randomized amount between two different min and max number values.

Example would be, you interact with this garbage can, collect 3 wire.
come back x time later (for demonstrations it’s 10 seconds), collect 27$.

My issue is on line 33 (if tbl[1] ~= nil then). It says im ‘Attempt to index number with number’.

If anyone knows anything please lmk!

Code:

local prox = script.Parent
local textBox = script.Parent.Parent.Parent.RewardProjection.BillboardGui.TextLabel -- Not in use...
local sound = script.Parent.Parent.TriggerSound

prox.Triggered:Connect(function(plr)
	prox.Enabled = false
	
	--------------------------------------------------------------------
	
	local Cash = plr.PlayerInfo.Inventory.Collectables.Cash
	
	local MinCash = 10
	local MaxCash = 150
	local AdditionalMultiplyerCash = math.random(MinCash,MaxCash)


	local Nails = plr.PlayerInfo.Inventory.Collectables.Nails

	local MinNails = 5
	local MaxNails = 10
	local AdditionalMultiplyerNails = math.random(MinNails,MaxNails)
	
	
	local Wire = plr.PlayerInfo.Inventory.Collectables.Wire
	
	local MinWire = 2
	local MaxWire = 4
	local AdditionalMultiplyerWire = math.random(MinWire,MaxWire)

--------------------------------------------------------------------
	local function RandomizedTable(tbl)
		local returntbl= {Cash.Value, Nails.Value, Wire.Value}
		if tbl[1] ~= nil then
			for i=1,#tbl do
				table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
				print("Randomized Items")
			end
			if tbl == 1 then
				Cash.Value = AdditionalMultiplyerCash
				print("Cash")
			end
			if tbl == 2 then
				Nails.Value = AdditionalMultiplyerNails
				print("Nails")
			end
			if tbl == 3 then
				Wire.Value = AdditionalMultiplyerWire
				print("Wire")
			end
		end
		print("Done")
		return returntbl
	end
	
	sound:Play()
	RandomizedTable()
	wait(10)
	prox.Enabled = true
	
end)

<3 thank u

1 Like

A couple of things:

  • You’re not storing the return result.
  • You’re not passing anything to the function which is expecting one parameter (tbl)
    Not sure why this would cause “attempt to index number with number” though

Hope this helps :slight_smile:

1 Like

Ah, I knew this was more complicated than I anticipated… Do you know how to solve said issues? Or can refer me to a link or article explaining how to do those things?

I’m sure once I figure out that problem, the ‘Attempt to index number with nil’ will be more clear.

Sorry, but I’m not too sure on what tbl is supposed to be in the function. Can you explain please?

To store the return results from it, just do:
local variableNameHere = RandomizedTable()

1 Like

Oh! my apologies,

tbl (what I thought and think) is the naming convention for what will be called upon. I figured it was the same as placing ‘Player’.

When the function ‘RandomizedTable’ is called, I figured ‘tbl’ would be what It’s going to get its information from.

right? I could be totally wrong though, I taught myself how to code by trial and error lol. Sometimes the forums can get a bit confusing. Though it’s a double edged sword at times xD.

Sorry, I’m kinda tired rn so I can’t really see a fix rn I’ll look when im less tired :smiley:

Oh no worries! If you can help It’ll be very appreciated! But alternatively don’t feel pressured :smiley:

1 Like

ProximityPrompt.Triggered returns the player that the prompt was triggered by as the first parameter. That is why you are able to define plr as a parameter when you connect the function to the Triggered event. As for RandomizedTable, it is not linked to any event that returns a value and thus if you have a parameter (tbl in this instance) you must pass it through yourself when you call the function:

-- Lets say "tbl" is an array:
local tbl = {"Cash", "Nails", "Wire"} -- Just an example

-- Whenever you call RandomizedTable, you would send this variable as the parameter
RandomizedTable(tbl)

Anyways, I have an even bigger question. What exactly do you want RandomizedTable to achieve? From the name, it sounds like you want it to return a randomized table consisting of the player’s Cash, Nails, and Wires but you also check if tbl is equal to 3 different numbers (which will never be true for any of them). If you can elaborate on that, that would be great!