Hey, can someone help me make a part counter?

Hey! So I’m making a lag test game, and I was wondering how I could make a part counter(local part counter) for the parts. Thanks :slight_smile:

So what it would do is when I click the game.StarterGui.LagTest.Start it will locally make the part counter visible and the parts start counting

I’ve made it so that when you click the start button it will make a folder in the workspace and everyone 0.2 seconds a part spawns inside it…

1 Like
local part_count = #workspace.PartsFolder:GetChildren()

And then you could set a TextLabel's text to that.

Didn’t work…

I’ve done this

“script.Parent.MouseButton1Click:Connect(function()
local folder = Instance.new(“Folder”)
folder.Parent = game.Workspace
while true do
local part = Instance.new(“Part”)
local partcounter = game.StarterGui.PartCounter
local partcountertext = partcounter.TextLabel
local part_count = #workspace.Folder:GetChildren()
partcounter.Enabled = true
partcountertext = part_count
wait(0.1)
part.Parent = folder
part.Anchored = false
part.Transparency = 0.5
end
end)”

then to stop

“script.Parent.MouseButton1Click:Connect(function()
local folder = game.Workspace:FindFirstChild(“Folder”)
folder:Destroy()
local partcounter = game.StarterGui.PartCounter
local partcountertext = partcounter.TextLabel
partcounter.Enabled = false
end)”

Can you still help?

Ok, so I figured out that it is changing the parts number, but its not showing me the text it says visible & the PartCounter has enabled set to true so don’t know whats going on.

This is not a great approach for what you’re doing, every time you click on the button a new while loop is initiated and will never stop running. On top of that, you create a new folder every time it runs which explains why the code I provided doesn’t seem to work in your case. Let me tidy up your code a bit:

local part_storage = Instance.new("Folder")
part_storage.Name = "PartCounter"
part_storage.Parent = workspace

local is_running = false;

local function Reset()
for _, p in pairs(part_storage:GetChildren()) do
p:Destroy()
end
end

local function Create_Part()
local part = Instance.new("Part")
part.Transparency = .5
part.Parent = part_storage
end

local function Begin()
local timer = tick()
while is_running do
if tick() - timer >= .1 then
timer = tick()
Create_Part()
end
end
end

script.Parent.Activated:Connect(function()
is_running = not is_running
if is_running then
Begin() --//Begin part generation
else
--//When the player disables the generating parts
Reset()
end
end)

I’ve restructured your code a bit, see how this works out.

1 Like

If you are mass creating parts from the client the easiest way would be to store the value in a variable and add to it each time you make a new part

local PCount = 0

– your part gets created
PCount = PCount + 1

This means you wont have to loop through all the parts and can easily reset it.