Text Label Not Updating Correctly

You can write your topic however you want, but you need to answer these questions:
I am making this simple game where every click is one part that is added to the game, but my issue is that i have a counter that counts how many parts there are, but it doesn’t seem to be working correctly, as it starts off with 20 parts, even when there has been none spawned in.

I have looked everywhere on DevForum, and I still don’t really know why it is happening, but I suspect that it is because there is already 20 parts in the workspace, but that doesn’t seem to be the issue, since there isn’t 20 parts in the workspace, and there is only a Camera, Terrain, SpawnLocation, and Baseplate.

Here is my script:

local label = script.Parent
local totalParts = 0  

local function updatePartCount()
	totalParts = 0  

	local parts = game.Workspace:GetDescendants()

	for _, part in pairs(parts) do
		if part:IsA("BasePart") then
			totalParts = totalParts + 1
		end
	end

	label.Text = "Total Parts: " .. totalParts
end

game.Workspace.DescendantAdded:Connect(function(obj)
	if obj:IsA("BasePart") then
		totalParts = totalParts + 1  
		updatePartCount()  
	end
end)

-- When a part is removed, update the count
game.Workspace.DescendantRemoving:Connect(function(obj)
	if obj:IsA("BasePart") then
		totalParts = totalParts - 1 
		updatePartCount()  
	end
end)

updatePartCount() 

Hey hey @DRZZ40000 !

Have you tried making a folder and putting the parts in there instead of checking plainly the Workspace? The Workspace has parts that are derived from BasePart and it counts them in as well.

Hi, thanks for the reply, I will try that.

Thank you, it worked, I didn’t know that was the issue.