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()