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

How to get 1 child instead of multiple in a :GetChildren() loop??

Asked by
ADUPS 5
6 years ago
Edited 6 years ago

basically, i made a generic loop that finds int values, and if their Value is 0 then it does things.

How can I only do things for only 1 of them? child[number] returns an error that says number is not a valid member of child.

FindFirstChildOfClass() also doesn't work because I need to find one with a specific value of 0.

help pls

part of the script:

for i,c in pairs (some directory:GetChildren()) do
    if c:IsA("IntValue") then
        if c.Value==0 then
            -- idk what to do now
        end
    end
end
0
Please include the script in your post so that we could be able to help you. LeadRDRK 437 — 6y
0
okay i did ADUPS 5 — 6y

2 answers

Log in to vote
1
Answered by
spr_ead 47
6 years ago
Edited 6 years ago

Try this:

function getNumber(tbl)
      for i,v in pairs(tbl) do
             if v==0 then
                    return i,v
             end
      end
      return nil
end

--[[The getNumber function will use a generic for loop to go through all the values of **tbl**, and will return the key and value of a value if it's 0. The function will end when any value in **tbl** is 0, or when the for loop goes through all values in the table. If there are no values found that are equal to 0, the function will return nil
--]]

--Usage

local Table = {
        1,
        2,
        0,
        3,
        4
}

local k,v = getNumber(Table)
print(k,v) -- Will print 3,0, because a 0 has been found in the 3rd key of the table, and 0 is its value. If the 3rd key was 5, the function will return nil, nil.

For what you want, you could set tbl to the values of the IntValues:

local tbl = {}
for _,v in pairs(something:GetChildren()) do
       if v:IsA('IntValue') then
              tbl[#tbl+1] = v.Value
       end
end
0
It works, thank you! ADUPS 5 — 6y
0
Np :). Please accept this answer if it helped you spr_ead 47 — 6y
0
BTW just asking - whats with the unknown global "i" in a lot of scripts that still work? ADUPS 5 — 6y
0
and how do i accept lol ADUPS 5 — 6y
View all comments (3 more)
0
I fixed it, just check the script. The reason why is because in the for loop line, I put an underscore as the key, and for the return on line 4 it has 'return i,v', and 'i' wasn't the key. But I fixed it spr_ead 47 — 6y
0
Below the answer, there's an Accept Answer button spr_ead 47 — 6y
0
hmm i only see the report button. Maybe because i joined like 30 minutes ago? Xd ADUPS 5 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You could just have a Boolean and only do stuff if it is false and once it does stuff to the first value it gets set to true like so:

Local boolCheck = false
for i,c in pairs (some directory:GetChildren()) do
    If boolCheck == false then -- continue searching if it hasn't found a zero yet
        if c:IsA("IntValue") then
            if c.Value==0 then
                BoolCheck = true -- change to false to stop iterating
            end
        end
    end
end

Answer this question