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:

1for i,c in pairs (some directory:GetChildren()) do
2    if c:IsA("IntValue") then
3        if c.Value==0 then
4            -- idk what to do now
5        end
6    end
7end
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:

01function getNumber(tbl)
02      for i,v in pairs(tbl) do
03             if v==0 then
04                    return i,v
05             end
06      end
07      return nil
08end
09 
10--[[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
11--]]
12 
13--Usage
14 
15local Table = {
View all 24 lines...

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

1local tbl = {}
2for _,v in pairs(something:GetChildren()) do
3       if v:IsA('IntValue') then
4              tbl[#tbl+1] = v.Value
5       end
6end
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:

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

Answer this question