Taking Time off a Looping Countdown

I am trying to make this lose 10s off the countdown once a player touches a part, my solutions so far have lead the countdown/values to go into negatives then straight away pick back up where it left off. For example it would go to something like -90 then right back to where it was which is no good :sad: .

Other than this small issue, the countdown works fine until I try to complete this ^

Has anybody got any ideas on how I’d do this?

local function Countdown()
		print("COUNTDOWN")
		while wait() do
			for i = gameplayTime, 1, -1 do
				gameStarted.Value = true
				wait(1)
				countdownV.Value = i
				gameStatus.Value = "In game" 
			end
		end
	end

thanks :smiley:

Remove while function.

local function Countdown()
		print("COUNTDOWN")
			for i = 1, gameplayTime, -1 do

				task.wait(1)
				countdownV.Value = gameplayTime - i

			end
		gameStarted.Value = true
		gameStatus.Value = "In game" 
	end
local function Countdown()
	print("COUNTDOWN")
	gameStarted.Value = true
	gameStatus.Value = "In game"
	for i = 0, gameplayTime do
		countdownV.Value = gameplayTime - i
		task.wait(1) 
	end
end

thanks, to remove the 10 seconds when the touched event happens, what should I do just to be clear?

you want to have a countdown and when the part get touched countdown goes down by 10?

yes, 10 seconds off the remaining time, if there was for example, 100 seconds left on the countdown, someone touches the part, it goes down by an additional 10 and continues - repeating for every time someone touches said part. thanks for your help

alright but one more question gameplayTime is a NumberValue or just a variable

local gameplayTime = 120

exactly what you sent there, spot on, sorry for not mentioning that in my original post

local function Countdown()
    countdownV.Value = gameplayTime
    local Connection
    Connection = Part.Touched:Connect(function() 
         countdownV.Value += -10 
    end)
    while countdownV.Value >= 0 do
         countdownV.Value += -1
         task.wait(1)
    end
    Connection:Disconnect()
end

would I need a debounce in there? thanks for ur help again

local function GameplayCountdown()
	
		countdownV.Value = gameplayTime
		gameStatus.Value = "In game"
		
		local Connection
		
		Connection = maps.ObbyLevels:FindFirstChildWhichIsA("Model").FinishedPart.Touched:Connect(function() 
			print("touched countdown")
				print("countdownAdjust")
				countdownV.Value = countdownV.Value - 10
			end
		end)
		
		while countdownV.Value >= 0 do
			countdownV.Value += -1
			task.wait(1)
		end
		Connection:Disconnect()
	end

gave it a go, nothing prints and nothing happens, got any more ideas? I know the player is touching the part because other parts of the script are working fine when the player touches the part.

no errors either

what is that second end after the countdownV.Value = countdownV.Value - 10
also did u call the function?

GameplayCountdown()
1 Like

yes the function is called, the timer goes down like it should, nothing prints in the touched event

other end was a typo, ignore it sorry

What does the code look like now?

this might be because of some code in your script
image
because it works fine for me
this also could be because of FinishedPart.CanTouch is set to false

1 Like

thats weird, and when you touch the part it subtracts 10?

try connecting another part to Touched event

1 Like
local function GameplayCountdown()
	
		countdownV.Value = gameplayTime
		gameStatus.Value = "In game"
		
		local Connection
		
		Connection = maps.ObbyLevels:FindFirstChildWhichIsA("Model").FinishedPart.Touched:Connect(function() 
			print("touched countdown")
				print("countdownAdjust")
				countdownV.Value = countdownV.Value - 10
			end
		end)
		
		while countdownV.Value >= 0 do
			countdownV.Value += -1
			task.wait(1)
		end
		Connection:Disconnect()
	end

I just thought I would mention this, make sure you add a debounce or it will be spammed.
Or if you only want it to decrease by ten seconds once, make sure you disconnect the event after it has fired.

still nothing sadly, not sure from here