Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

Please include the script in your post so that we could be able to help you.

0

okay i did

2 answers

✓ Accepted answer

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

7 comments

0

It works, thank you!

0

Np :). Please accept this answer if it helped you

0

BTW just asking - whats with the unknown global "i" in a lot of scripts that still work?

0

and how do i accept lol

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

0

Below the answer, there's an Accept Answer button

0

hmm i only see the report button. Maybe because i joined like 30 minutes ago? Xd

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

This discussion is preserved as it was. New answers, comments, and votes are closed.