Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make a simple script with loops to search through values of an object?

Asked by 7 years ago

For example I have 3 Slot Frames in my GUI, they are named Slot1, Slot2, Slot3. Each contains a BoolValue named InUse, I want to search through all 3 of these Slots so if Slot1 is in use then continue to the next one, if it's not in use then Place stuff inside.

The simple way (would be longer and harder) would be like this;

local Slot1 = script.Parent.Parent.Slot1
local Slot2 = script.Parent.Parent.Slot2
local Slot3 = script.Parent.Parent.Slot3

script.Parent.MouseButton1Down:connect(function()
    if Slot1.InUse.Value == true then
        --Go to slot 2?
else
    if Slot.InUse.Value = false then
        --My code
end)

But this would take forever if the slots are being added, (which they will in game feature.)

Please help me ;-;

0
Is this for a backpack system? User#5423 17 — 7y
0
Yea BlackOrange3343 2676 — 7y

2 answers

Log in to vote
0
Answered by
saenae 318 Moderation Voter
7 years ago

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
    -- Stuff
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  -- It's a good idea to make this a variable
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
    -- Stuff
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
    -- Stuff
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 -- Check the first 4 characters of the object
        -- stuff
    end
end

Now that you're looping through and getting your slots, the rest is easy.

local Button = script.Parent -- Making this a variable isn't a bad idea either :P
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 -- Check the first 4 characters of the object
            if(Slot.InUse.Value == false) then
                -- Your Code
                break -- stops the loop so that you don't go to the other slots
            end
--[[ If 'InUse' is not false, the loop won't break, and will instead take you to the other slots ]]--                           
        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 :)

Ad
Log in to vote
0
Answered by
RoJiyn 12
7 years ago

Hi I would do it this way:


local Slots = script.Parent.Parent:GetChildren() --- it gets all the children which is the slots and put it into a table. -------------------------Conditions script.Parent.MouseButton1Down:connect(function() for i = 1, #Slots do ------- Use the for loop like this : for iterator_variable = start value, end value, increment do (from Roblox Wiki:http://wiki.roblox.com/?title=Loops#For) ---- Basically, it will give the var a number and #Slots return the Number of objects in the table. if Slots[i].InUse.Value == false then -- See Below --Code Here elseif Slots[i].InUse.Value == true then -- this is optional as the script specify what to do if false and will do nothing if true as it is not specify if you did not put this line return nil -- do nothing (Optional) end -- optional end end)

Basically The:GetChildren() will return it like this:

local Slots = {script.Parent.Parent.Slot1,script.Parent.Parent.Slot2,...}

Slots[i] The var i is the column number. Thus, Slots[1] = script.Parent.Parent.Slot1

0
Hope this helps RoJiyn 12 — 7y
0
See saenae answer as I forgot to tell it to check that it is actually slots. RoJiyn 12 — 7y

Answer this question