- I am making a custom BackpackGui, and im using OOP for it.
- The issue is that in the constructor, the
lua for i = 1, #O.inputOrder do
replace the existing object of a reference with another one because the indices have the same name:
--constructor
function BackpackGui.new(player)
local O = {}
--Instance fields
O.inputKeys = {["One"] = {txt = "1"},["Two"] = {txt = "2"},["Three"] = {txt = "3"},}
O.inputOrder = {O.inputKeys["One"],O.inputKeys["Two"],O.inputKeys["Three"],}
O.toShow = #O.inputOrder
BackpackGui.totalX = (O.toShow*BackpackGui.iconSize.x)+((O.toShow+1)*BackpackGui.iconBorder.x)
BackpackGui.totalY = BackpackGui.iconSize.y+(2*BackpackGui.iconBorder.y)
O.ScreenGui = Instance.new("ScreenGui")
O.ScreenGui.Name = "BackpackGui"
O.ScreenGui.Parent = player.PlayerGui
--Now creates a a container for all the slots
O.container = Instance.new("Frame")
O.container.Name = "Container"
O.container.Position = UDim2.new(0.5, -(BackpackGui.totalX/2), 1, -(BackpackGui.totalY+(BackpackGui.iconBorder.y*2)))
O.container.Size = UDim2.new(0, BackpackGui.totalX, 0, BackpackGui.totalY)
O.container.BackgroundTransparency = 1
O.container.ZIndex = 1
O.container.Parent = player.PlayerGui.BackpackGui or player.PlayerGui:WaitForChild("BackpackGui")
--now creates the slots and place them inside the container
for i = 1, #O.inputOrder do
local value = O.inputOrder[i]
local tool = value["tool"]
O.template = Instance.new("Frame")
O.template.Name = value["txt"]
O.template.Size = UDim2.new(0, BackpackGui.iconSize.x, 0, BackpackGui.iconSize.y)
O.template.Position = UDim2.new(0, (i-1)*(BackpackGui.iconSize.x)+(BackpackGui.iconBorder.x*i), 0, BackpackGui.iconBorder.y)
O.template.BackgroundColor3 = Color3.fromHSV(0, 0, 0)
O.template.BorderColor3 = Color3.fromHSV(0.119333, 0.400992, 0.792157)
O.template.BorderSizePixel = 4
O.template.ZIndex = 2
O.template.Parent = player.PlayerGui.BackpackGui.Container or player.PlayerGui.BackpackGui:WaitForChild("Container")
O.imageLabel = Instance.new("ImageLabel")
O.imageLabel.Size = UDim2.new(1,0,1,0)
O.imageLabel.Position = UDim2.new(0,0,0,0)
O.imageLabel.BackgroundColor3 = Color3.fromHSV(0, 0, 0)
O.imageLabel.ZIndex = 3
if tool then
O.imageLabel.Image = tool.TextureId
end
O.imageLabel.Parent = player.PlayerGui.BackpackGui.Container[value["txt"]] or player.PlayerGui.BackpackGui.Container:WaitForChild(value["txt"])
O.textLabel = Instance.new("TextLabel")
O.textLabel.Size = UDim2.new(0.4, 0, 0.4, 0)
O.textLabel.Position = UDim2.new(0 ,0, 0.6, 0)
O.textLabel.Text = value["txt"]
O.textLabel.Font = "SourceSans"
O.textLabel.TextColor3 = Color3.fromHSV(0, 0, 1)
O.textLabel.TextScaled = true
O.textLabel.BackgroundTransparency = 1
O.textLabel.ZIndex = 4
O.textLabel.Parent = player.PlayerGui.BackpackGui.Container[value["txt"]]
end
setmetatable(O, BackpackGui)
return O
end```
As you can see, the reference points to a new object every iteration. I want it to point to the object of its iteration.
3. I have tried thinking about solution like giving a different index name each iteration and using two tables, but its not satisfactory.