Hey, BlackOrange!
An easy way to iterate through any table is using the generic for loop, as seen below:
for index, value in pairs(Table) do
end
As you might expect based on the parameters, it gives you both the index and the value (each of which can be of any data type using the 'pairs' notation).
Keep in mind: The index and value variables can be named anything you want
In order to use it in your case, you'll need to get a table of values that include your Slots, using the GetChildren method like so:
local SlotHolder = script.Parent.Parent
local Slots = SlotHolder:GetChildren()
Now you can just throw this into your for loop!
local SlotHolder = script.Parent.Parent
local Slots = SlotHolder:GetChildren()
for index, Slot in pairs(Slots) do
end
If you'd like, you can also get rid of that Slots variable and just throw it straight into your for loop:
local SlotHolder = script.Parent.Parent
for index, Slot in pairs(SlotHolder:GetChildren()) do
end
You may also want to check that everything in 'Slots' is actually a slot:
local SlotHolder = script.Parent.Parent
for index, Slot in pairs(SlotHolder:GetChildren()) do
if(Slot.Name:sub(1, 4) == "Slot") then
end
end
Now that you're looping through and getting your slots, the rest is easy.
local Button = script.Parent
local SlotHolder = script.Parent.Parent
Button.MouseButton1Down:connect(function()
for index, Slot in pairs(SlotHolder:GetChildren()) do
if(Slot.Name:sub(1, 4) == "Slot") then
if(Slot.InUse.Value == false) then
break
end
end
end
end
And you're done! Feel free to add any number of slots you want (so long as the slot's name starts with 'Slot').
Hope this helps :)
2 comments
Is this for a backpack system?
Yea