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

Am I using #Obj correctly?

I'm writing a script that is supposed to find the number of objects in a model with a certain name and return the number to me. I have attempted this but I keep getting the error - "attempt to index upvalue 'PACK' (a nil value)" How can I fix this?

local player = game.Players.LocalPlayer
local PACK = player:findFirstChild("Pack")
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

function check_Have()
	
	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 = #name
	end

	print(count)
	end
	end

--function get_Have()
	--end

function onMouseButton1Down(mouse)
	
	
	FRAME.Visible = (not FRAME.Visible)
	
	check_Have()
	
end

BUTTON.MouseButton1Down:connect(onMouseButton1Down)

1 answer

✓ Accepted answer

There error has nothing to do with your use of #.

The error is telling you that PACK is nil.

Since PACK is only set on line 2, this means that player:FindFirstChild is returning nil. This means the object did not exist at that time.

If you are adding this object to the player, you should probably use :WaitForChild("Pack") since this LocalScript might run the other script adds it.


:findFirstChild is deprecated. Use :FindFirstChild instead.


Tab your code correctly. It makes it much easier to read, understand, and edit.

1 comment

0

perfect! thank you!

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