Can't add time to coroutine timer

Made a timer using RunService.Heartbeat:wait() . Timer counts down as intended, however when I attempt to increase the time remaining, nothing changes:

local RunService = game:GetService("RunService")
local timeSettings = workspace.TimeSettings -- Folder in workspace

-- Increase the time by 100
function addTime()
	timeSettings:SetAttribute("TimeRemaining", timeSettings:GetAttribute("TimeRemaining") + 100)
	print(timeSettings:GetAttribute("TimeRemaining")) -- Print time remaining after increase
end

-- Countdown using Heartbeat
coroutine.wrap(function()
	while timeSettings:GetAttribute("TimeRemaining") > 0 do
		timeSettings:SetAttribute("TimeRemaining", timeSettings:GetAttribute("TimeRemaining") - RunService.Heartbeat:Wait())
	end	
end)()

-- Increase the time every 5 seconds
while wait(5) do
	addTime()
end

Strangely enough the print statement in the addTime function outputs the correct time remaining, however the TimeRemaining attribute doesn’t change:

1 Like

Could this possibly be a race condition?

What happens if you add a wait(6) in the loop you have in your coroutine?

I’m also not exactly sure what you’re trying to do here, but I’d think there’d certainly be a better way to handle a timer.

local runService = game:GetService("RunService")
local currentTime = 10
local addingTime = false --//Prevent race conditions?

local function addTime(t)
addingTime = true

wait(.001) --//Delay for a very short amount of time to prevent race conditions. might be pointless not sure

currentTime += t
addingTime = false
end

local function handleTimer()
local updateListener;

updateListener = runService.Heartbeat:Connect(function(dt)
if addingTime then return end

currentTime -= dt

if currentTime <= 0 then --//Timer ended
updateListener:Disconnect()
--//do stuff
end)
end
1 Like

It’s very strange that it don’t actually update the attribute… I made some changes to your coroutine, but i don’t know if this will work.

local RunService = game:GetService("RunService")
local timeSettings = workspace.TimeSettings -- Folder in workspace

-- Increase the time by 100
function addTime()
	timeSettings:SetAttribute("TimeRemaining", timeSettings:GetAttribute("TimeRemaining") + 100)
	print(timeSettings:GetAttribute("TimeRemaining")) -- Print time remaining after increase
end

-- Countdown using Heartbeat
coroutine.wrap(function()
	while true do
        local val = timeSettings:GetAttribute("TimeRemaining")
        if val <= 0 then
            break
        end
        local newVal = val - RunService.Heartbeat:Wait()
        timeSettings:SetAttribute("TimeRemaining", newVal)
	end	
end)()

-- Increase the time every 5 seconds
while wait(5) do
	addTime()
end

Edit: the other solution i can think of is to have a number value (it can be any number value, you can just create one in the script and don’t actually make a parent for it, so it won’t be somewhere in the game) then instead of setting attribute you can set this number value. Additionally if you need that attribute somewhere else you can connect a .Changed event to number value and set the attribute value to number value’s value.

I’m attempting to add 100 to a countdown timer every 5 seconds, adding a wait(6) to the coroutine loop would obviously mess up the countdown itself.

No change, unfortunately. The timer still doesn’t increase,

Could you perhaps try the idea i offered in the Edit? Or you have some issues with it?

Did you try the code I suggested? Any different end result?