How can I make my if statement run properly inside a GetDescendants() for loop?

Hi all!

I am trying to figure out how I can get my if statement to run only if the player does not have a tool with a NumberValue descendant inside of it. Right now, the if statement is running because the for loop would be determining that all other descendants are not a NumberValue. How can I make it disregard everything but the NumberValue? Here is the if statement:

	local ctools = character:FindFirstChildOfClass("Tool")
    local weldedpart = script.Parent.Parent.Parent
    local button = script.Parent

for _,childpack in pairs(backpack) do
			if childpack.ClassName ~= "NumberValue" and not ctools then
				local v = tostring(weldedpart:FindFirstChildOfClass("Model"))
				local item = weldedpart:FindFirstChildOfClass("Model")
				button.ObjectText = "Place"
				local thing = ReplicatedStorage.Tools[v]:Clone()
				thing.Parent = character
				thing.Price.Value = item.Price.Value
				thing.Top.BillboardGui.TextLabel.Text = "$"..item.Price.Value
				welded = false
				item:Destroy()
			end
		end

This isn’t the whole script, but I tried to add everything I found that was relevant.

For context, I am trying to make it so that if the player has a tool in their backpack that contains a NumberValue, I do not want this part of the script to run. The problem is, using GetDescendants() means that the tool also contains parts that are not a NumberValue, which is making that part of the if statement return “true”.

Many thanks in advance!

Try doing:

for _, tool in pairs(backpack:GetChildren()) do
	if not tool:FindFirstChildWhichIsA("NumberValue", true) and not ctools then
		... --- your code
	end
end

It will only do the if-statement if there is no numbervalue inside the tool

1 Like

Yea the best option like @ifkpop said would be to use GetChildren() and to check if the tool has a number value in it.

GetDescendants() gets everything that is below the backpack, including all of the objects in anything in the players inventory, while GetChildren() just gets what is directly parented to the backpack

But doesn’t this have the same issue as my code? If the player has more than 1 item in their backpack and that other item doesn’t have a NumberValue, then the block you’ve used will run the code as it has detected that the other item does not have a NumberValue attached to it.

I don’t think I made it as clear as I could have in the original post, but the problem I’m having is that if the player has multiple tools (for example, 2 swords that don’t contain a NumberValue and then a different tool that contains a NumberValue), then the code is running anyway, as the 2 swords do not contain a NumberValue. My goal is to make the code not run if there is a tool in the player’s backpack that contains a NumberValue fullstop. If the player has tools that do not contain NumberValues, it should still not run the code as a tool exists in the player’s backpack that does have a NumberValue.

Simple fix modifying @ifkpop code.

local numValFound = false
for _, tool in pairs(backpack:GetChildren()) do
	if tool:FindFirstChildWhichIsA("NumberValue", true) then
		numValFound = true
	end
end
if not numValFound then
    --your code
end

[Edit] Or thinking about it, you could just use:

local numValFound = false
for _, item in pairs(backpack:GetDescendants()) do
	if item:IsA("NumberValue") then
		numValFound = true
	end
end
if not numValFound then
    --your code
end

This works great! I can’t believe I didn’t think of this… I have done the exact same thing to solve a similar problem in the same script. Thank you!

1 Like

I’d say the first alternative is better, I’d also say you should add break when it has been found as to not continue looping through stuff for no reason.

local numValFound = false
for _, tool in pairs(backpack:GetChildren()) do
	if tool:FindFirstChildWhichIsA("NumberValue", true) then
		numValFound = true
		break -- Stop the for loop, there is nothing more to be done here
	end
end
if not numValFound then
    --your code
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.