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

How do I get a number from for table?

Asked by 8 years ago
Edited 8 years ago
function checksamount()
for _,snipers in pairs(workspace:GetChildren())do
        if snipers.Name == "Soldier3" and snipers:FindFirstChild("Owner") and snipers.Owner.Value == player then
            return #snipers
        end
    end
end

So from the for loop, I fetch the children of workspace. Then I filter all of the children through the if statement to get every object called soldier3. Now I end up with a variable with all of the soldier3's. How do I get the number of soldier3's in the variable?

1
koolkid your answer doest match the question, sorry but there is nothing you can do about it crut24 50 — 8y
0
thanks laughablehaha 494 — 8y

2 answers

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

From what I can tell, you're trying to find the number of instances in 'workspace' that meet the conditions in the if block. You can do this by using a local variable to count them.

function checksamount()
    local snipersNum = 0
    for _,snipers in pairs(workspace:GetChildren())do
            if snipers.Name == "Soldier3" and snipers:FindFirstChild("Owner") and snipers.Owner.Value == player then
            snipersNum = snipersNum + 1
        end
    end
    return snipersNum
end

Putting a return statement in the if block will end the function as soon as the condition is met by one of the children, even if the loop is not complete.

0
thanks!!! ^.^ laughablehaha 494 — 8y
Ad
Log in to vote
1
Answered by
crut24 50
8 years ago
Edited 8 years ago

function checksamount() for _,snipers in pairs(workspace:GetChildren())do if snipers.Name == "Soldier3" and snipers:FindFirstChild("Owner") and snipers.Owner.Value == player then local pat = "%d+" return tonumber((snipers.Name):match(pat)) end end end

it uses a String Pattern to find the last numbers at the end and make them into a number. Goodluck :D

Answer this question