Trouble Updating IntValue Within targetObject

Hi there,

I’ve recently encountered an issue within my script that involves an IntValue that’s made a child of every part indexed in the workspace named “targetObject”, having that IntValue store a count all of the children within such targetObject, and updating the count every so often(I’ve tried 3 seconds with a while wait(3) do loop and more, none of my methods work). My problem is that no matter the loop I try, I cannot get the count to update every so often. My desired outcome is to get the count that is stored inside of the IntValue to be updated every so often, maybe every second if possible.

Script:

local workspaceDescendants = workspace:GetDescendants()

function distributeObjects()
	for index, descendant in pairs(workspaceDescendants) do
		local childCounter = Instance.new("IntValue") -- creates an IntValue that will be in every descendant that I've defined.
		childCounter.Name = "childCounter"
		childCounter.Value = 0
		if descendant:IsA("Part") and descendant.Name == "targetObject" then
			childCounter.Parent = descendant -- defines a descendant(targetObject).
		end
		--[[ I've tried while wait loops and other unlisted loops in order to attempt to
		store the number of children within a targetObject to a childCounter inside
		of a targetObject from a count that is initiated every so often utilizing the 
		for loop underneath this comment.
		--]]
		-- ex. while wait(3) do
		local descendantChildren = descendant:GetChildren()
		for index, child in pairs(descendantChildren) do
			childCounter.Value = childCounter.Value + 1
		end
		-- end (this wouldn't work)
	end
end

distributeObjects()

First off, when you make a while loop, it would cause the code to forever stuck on the iteration of the loop, so you should add a spawn() surrounding the interior of the exterior for loop like this:

local workspaceDescendants = workspace:GetDescendants()

function distributeObjects()
	for index, descendant in pairs(workspaceDescendants) do
		spawn(function()
			if descendant:IsA("Part") and descendant.Name == "targetObject" then
				local childCounter = Instance.new("IntValue") -- creates an IntValue that will be in every descendant that I've defined.
				childCounter.Name = "childCounter"
				childCounter.Value = 0
				childCounter.Parent = descendant -- defines a descendant(targetObject).
				while wait(1) do
					local descendantChildren = descendant:GetChildren()
					for index, child in pairs(descendantChildren) do
						childCounter.Value = childCounter.Value + 1
					end
				end
			end
		end)
	end
end

distributeObjects()

Second, I want to ask if you are storing the number of children in the IntValue? Or are you just adding to the IntValue every second or so (Ex: with 10 children, the values would go on like 10, 20, 30, …). Because if you are just counting the number of children, you need to reset the value of the variable back to 0 every time at the beginning of the while loop. Next, if you are actually just storing the number of children in the IntValue, then you know you could just do something like #descendant:GetChildren() right? Finally, if you want to get an accurate second, I suggest using os.clock() and RunService.Heartbeat:Wait() (you can look into those on the API pages), but if accuracy doesn’t matter, just use wait(1).

1 Like

In response to the question you asked within your reply, I am attempting to store the number of children within the targetObject into the IntValue(childCounter). Thanks for your reply, I’ll try the methods you’ve recommended within your reply and incorporate them within my code.

1 Like