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

How do I get a number from for table?

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?

2 comments

1

koolkid your answer doest match the question, sorry but there is nothing you can do about it

0

thanks

2 answers

✓ Accepted answer

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.

1 comment

0

thanks!!! ^.^


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

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