local player = game.Players.LocalPlayer local lightPanels = workspace:FindFirstChild("LightPanels") local panels = lightPanels:FindFirstChild("Panels"):GetChildren() panelsTable = {} function checkPanels() for i, panel in pairs(panels) do if panel.Material == Enum.Material.SmoothPlastic and not panelsTable[panel.Name] then table.insert(panelsTable,panel.Name) end end print(#panelsTable) print(#panels) if #panelsTable == #panels then print("success") end end game.ReplicatedStorage:FindFirstChild("PanelEvent").OnClientEvent:Connect(function(panel) local char = player.Character if char then local pingSound = char:FindFirstChild("PingSound") pingSound:Play() panel.Material = Enum.Material.SmoothPlastic end end) while wait(.5) do checkPanels() end
I am essentially trying to make a system whereby you touch a panel, then that panel changes material to SmoothPlastic and adds the panel in question to panelsTable (in function checkPanels).
However, everytime the function runs, the same panels keep getting added to the table. It seems as though 'and not panelsTable[panel.Name]' is not working? For example I touch 1 panel. everytime #panelsTable is printed every 0.5 seconds, that same panel is being added to the table, despite checking that it's not already in the table before adding it.
Would appreciate assistance
Tables are split into two components: the array component and the hash-table (dictionary) component.
table.insert
inserts the given element into the table's array component, which has the element sit at an aligned numerical index.
-- Visualization: local panelsTable = {"x Name"} -- 1 (index)
The code you've used attempts to locate a key-value pair by the panel's name; you're looking for data in the dictionary component:
-- That would require the following orientation of data: local panelsTable = { ["x Name"] = true }
To check if an element is present in an array, you have to perform a linear search across its elements. The table
library provides a function that does exactly that:
print(table.find(panelsTable, "x Name")) --> 1