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

How would I say that count is equal to the number of objects in a model with a specific name?

I'm trying to make this script find the number of objects with a specific name in a model. How can I fix my script to do this? It currently works but gives the number of objects in the model not just the ones with a specific name. Thanks for any help!

local player = game.Players.LocalPlayer

local BUTTON = script.Parent
local FRAME = BUTTON.Parent.Parent.Parent.ShowFrame1
local NBox1 = FRAME.ColumnFrame2.BoxFrame2
local NBox2 = FRAME.ColumnFrame2.BoxFrame3
local NBox3 = FRAME.ColumnFrame2.BoxFrame4
local NBox4 = FRAME.ColumnFrame2.BoxFrame5
local HBox1 = FRAME.ColumnFrame3.BoxFrame2
local HBox2 = FRAME.ColumnFrame3.BoxFrame3
local HBox3 = FRAME.ColumnFrame3.BoxFrame4
local HBox4 = FRAME.ColumnFrame3.BoxFrame5
local htext1 = HBox1.TextLabel
local ntext1 = NBox1.TextLabel
local htext2 = HBox2.TextLabel
local ntext2 = NBox2.TextLabel

function check_Have()
	
	local PACK = player:FindFirstChild("Pack")
	local PACKTAB = PACK:GetChildren()
	local name = FRAME.RowFrame2.TextLabel.Text
	local obj = PACK:GetChildren()
	local count = #obj
	

for index, child in pairs(PACKTAB) do
	if child.Name == name then 
		local count = #obj -- Here is the problem. How would I say that the count is the number 
		-- of objects with the name- name (the variable called name not literally "name")
		htext1.Text = count
end
end
	
	print(count)
end

1 answer

✓ Accepted answer

If you want to count things, you just count them.

If your job as a person is to count how many things in a bag are yellow, how would you do it?

You'd take one thing out of the bag, and [only] if it's yellow, you'd write down a tally mark.

You'd take out a second thing, and then you'd examine it the same way, and maybe write down a tally mark.

You'd do the same thing for the next, and the next, and the next thing, until the bag is empty.

In code, that looks like:

local tally = 0
for _, thing in pairs(bag) do
	if isYellow(thing) then
		tally = tally + 1
	end
end

Code

To be more concrete, in your case it would be

	local count = 0
	for _, child in pairs(PACK:GetChildren()) do
		if child.Name == name then
			count = count + 1
		end
	end

#Style

I highly recommend having better variable names than you're using.

obj is a terrible name for a list of objects.

Also, a local variable that's not used in the middle of a loop is just going to go away. I don't know why you've redefined local count as a second time in your script.

1 comment

0

Perfect answer, thank you

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