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?
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.
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