Making a timed button subtracts 23 each time instead of 1

I’m making a timed button and there is a GUI on screen that says “You have 50 seconds.” Obviously when the timer goes down that GUI goes down too, but I can’t seem to figure out a way to do it. I’ve tried to do repeat until but that just doesn’t work.

local deb = false
local TweenService = game:GetService("TweenService")
local partsfold = workspace.areas.Antarctica.AE.AE1
local currentime = 50
local plr = game.Players.LocalPlayer
local text = plr.PlayerGui:WaitForChild("Main").AEText.TextLabel
local button = workspace.areas.Antarctica.AEStart.ClickDetector
--local thing = 0

button.MouseClick:Connect(function()
	print("hi again")
	text.Visible = true
	button.Parent = workspace.areas.Antarctica.ICEEEE.clickdetectholder
	if not deb then
		deb = true
		for _,Part in partsfold:GetChildren() do
			task.spawn(function()
				Part.Transparency = 0
				Part.CanCollide = true
				repeat
					currentime -= 1
					text.Text = "You have "..currentime.." seconds."
					task.wait(1)
				until currentime == 40
				--task.wait(50)
				button.Parent = workspace.areas.Antarctica.AEStart
				Part.Transparency = 1
				Part.CanCollide = false
				text.Visible = true
			end)
		end
	end
end)

This is the current script, it’s a local script inside StarterPlayerScripts if that helps :man_shrugging:.



here are some photos of it, don’t even know why it’s doing this

Can you elaborate on the problem a little more? I can’t tell what the problem is.

If you want to make sure the numbers don’t go negative, you can place a check in your code.

if currentime < 0 then
   currentime = 0
end

Basically once I run the game and click the button, it subtracts 23 from the current time for some reason. It goes from 50 → 27 → 4, so on and so forth, when it should be subtracting only 1 per second.

you’re doing the countdown for every single part in the part folder, and for every part, it will start the countdown. at the start of the countdown, it removes 1 from currentime. Might i ask what you’re trying to do? I might be able to stitch together a working script for you.

Ohh I think I know what’s wrong. There’s 23 parts in the folder and it just subtracts that from the countdown every time. I just want it to subtract 1 every second instead of 23 every second.

I don’t generally know how to fix that though, cause the timer has to hit 0 before the other stuff occurs.

Every second, iterate through each part, and set the text there, wouldn’t spawn a new thread for each part.

repeat
    currentTime -= 1

    for _, Part in partsFold:GetChildren() do
        -- set the text for each part
    end
    
    task.wait(1)
until currentTime == 40
1 Like

This would work but the problem is that once the timer ends it changes the transparency of all the parts back to 1 and sets the collision of them to false, but since Part is defined in the for _, line it says Unknown global ‘Part’

In my loop I used a lowercase P not uppercase, so you’d have to switch the casing in your loop. I edited my post to include that as well

im confused, like i dont really understand what you mean by that (im new to this scripting thingamabomajiggy)

if not deb then
		deb = true
		repeat
			currentime -=1
			
			for _, Part in partsfold:GetChildren() do
				task.spawn(function()
					Part.Transparency = 0
					Part.CanCollide = true
				end)
			end
			
			task.wait(1)
		until currentime == 40
		button.Parent = workspace.areas.Antarctica.AEStart
		Part.Transparency = 1
		Part.CanCollide = false
		text.Visible = true

like i have this right now and i changed it, but it still doesnt work
image
heres an image too to show what i mean

Oh I see, I misunderstood what you meant. You’d have to create another loop at the end to do a final iteration over all the parts.

repeat
    currentTime -= 1

    for _, Part in partsFold:GetChildren() do
        Part.Transparency = 0 -- note that setting properties does not yield so adding a task.spawn here wouldn't have any benefit
	    Part.CanCollide = true
    end
    
    task.wait(1)
until currentTime == 40

for _, Part in partsFold:GetChildren() do -- we create a final loop once our timer is over
    Part.Transparency = 1
    Part.CanCollide = false
end

Also it may be advisable to move the for loop inside the repeat loop, outside of the repeat loop if you don’t need to iterate over every part every second. I thought each part had its own label and therefore had to be updated every second.

This works, thank you so much. I spent 30 minutes last night trying to figure out how to work this, and the fact that this is a replicable system makes this so much better :sob::sob:.

1 Like