Help with Array relations for a stock system

  1. What do you want to achieve?

Hello there, I’m working on a system to restock a shelf of goods in a game, by the player carrying boxes to a spot to unload them. The boxes are named accordingly, and have values to match the data needed in the shelf.

  1. What is the issue?

This is my first time using array oriented programming in Roblox, so I’m unsure as to what I’m doing wrong here. The code isn’t successfully relating the tools in the players backpacks to the predefined list of possible names for the stock.

Please see below for the code:

-- Server Variables
restockFolder = game.Workspace:WaitForChild("StockSystem")
stockEvent = game.ReplicatedStorage:WaitForChild("Events"):WaitForChild("RestockFire")

stockList = {
	{"IceCream"},
	{"Popsicles"},
	{"CannedFood"},
	{"Chips"},
	{"Coffee"},
	{"Lolipops"},
	{"MechParts"}
}

function restockThing(stockType,stockName)

	
	local currentPart = stockType.stockName.i
	local maxStock = restockFolder:WaitForChild(stockType):WaitForChild(stockName).MaxStock.Value
	local currentStock = restockFolder:WaitForChild(stockType):WaitForChild(stockName).StockLevel.Value

	local i = 1 + currentStock

	-- Break Function if already at max stock
	if i == maxStock then
		print("Already at Max Stock")
		return
	end

	-- Set parts to visible
	if currentPart:IsA("Model") then
		local parts = {}
		local function findParts(obj)
			for _, child in pairs(obj:GetChildren()) do
				if child:IsA("MeshPart") or child:IsA("Part") or child:IsA("UnionOperation") then
					table.insert(parts, child)
				end
				findParts(child)
			end
		end
		findParts(currentPart)
		for _, part in pairs(parts) do
			part.Transparency = 0
		end
	else 
		currentPart.Transparency = 0
	end


	-- Return new currentStock Value
	if currentStock == maxStock then
		return
	else if currentStock < maxStock then
			local newStock = currentStock + 1
			restockFolder:WaitForChild(stockType):WaitForChild(stockName).StockLevel.Value += 1
		end
	end
end

-------------------------------------------
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
-------------------------------------------


function thingy(plr)

	print("Restock Fired")

	local backpackItems = 0
	local backpackArray = {

	}

	for i,v in pairs (plr.Character:GetChildren()) do
		if v:IsA("Tool") then
			print(v.Name)
			backpackItems += 1
			table.insert(backpackArray, v)
		end
	end

	for i,v in pairs (backpackArray) do
		if table.find(stockList,v.Name) then
			objThing = v
			print(objThing.Name)
		end 
	end


	print("Firing Event")
	--stockEvent:FireServer(objThing.StockType.Value,objThing.Name)
	restockThing(objThing.StockType.Value,objThing.Name)
end


script.Parent.Triggered:Connect(function(player)
	thingy(player)
end)


stockList = {
	"IceCream",
	"Popsicles",
	"CannedFood",
	"Chips",
	"Coffee",
	"Lolipops",
	"MechParts"
}

try to remove the brackets from the stock list

	for i,v in pairs (backpackArray) do
		if table.find(stockList,v.Name) then -- here
			objThing = v
			print(objThing.Name)
		end 
	end

the reason being that when you try to find the name in the table you wont find anything because the names are all inside tables. let me know if it work!

1 Like

That did indeed fix the issue! Thank you!

Now just a few smaller things to fix.

1 Like

I just wanna say, your function names are great. But might be a pain for other scripters if there ever will be any. I’d suggest naming your functions with what they do

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.