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