1 | function checksamount() |
2 | for _,snipers in pairs (workspace:GetChildren()) do |
3 | if snipers.Name = = "Soldier3" and snipers:FindFirstChild( "Owner" ) and snipers.Owner.Value = = player then |
4 | return #snipers |
5 | end |
6 | end |
7 | 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?
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.
1 | function checksamount() |
2 | local snipersNum = 0 |
3 | for _,snipers in pairs (workspace:GetChildren()) do |
4 | if snipers.Name = = "Soldier3" and snipers:FindFirstChild( "Owner" ) and snipers.Owner.Value = = player then |
5 | snipersNum = snipersNum + 1 |
6 | end |
7 | end |
8 | return snipersNum |
9 | 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.
1 | function checksamount() |
2 | for _,snipers in pairs (workspace:GetChildren()) do |
3 | if snipers.Name = = "Soldier3" and snipers:FindFirstChild( "Owner" ) and snipers.Owner.Value = = player then |
4 | local pat = "%d+" |
5 | return tonumber ((snipers.Name):match(pat)) |
6 | end |
7 | end |
8 | end |
it uses a String Pattern to find the last numbers at the end and make them into a number. Goodluck :D