Making a timer that never stops, even when the game is inactive

You can write your topic however you want, but you need to answer these questions:

  1. Dear All, I hope everyone is fine. I want to ask if it is possible to make a timer that runs even or that runs when an Roblox player is inactive. Actually, I am making a game in which I want to run a timer that will let user to unlock things… Let’s say after 14 minutes.

Is it possible?? Any help would highly be appreciated! :smile:

I have already tried tweaking the normal way, but found lua is Very limited. i even tried looking at how different languages deal with it, but they go as far, as to having a calendar the game reads and tells by that. it would be amazing if i could get some help on the issue! This will support many future developers and hopefully release tons of new features all around games. This question is mainly to developers who make daily timers. how do you do that??

I would include scripts if i even had an idea how to do it. I understand its forbid to ask for full scripts but it would be most helpful. even just saying a method of how instead would be very useful. Thanks!

2 Likes

You would probably use Os.Time. It finds out the time since january long ago.

local StartTime = os.time()
wait(10)
local EndTime = os.time() - StartTime
print(EndTime) -- The time that has passed.

This would print 10 because 10 seconds have passed since we have checked the first and second time. You don’t have to be in game because os.time() adds 1 second globally.

ill test it and if i get it to work ill mark this reply as a solution. Thanks for the instant reply :smile::+1:

Forgot to mention but if you want to leave and then rejoin and check how much time has passed you will need to save the start time using a Datastore.

alright. also im going to have to connect it with a gui which shouldn’t be to hard

No, but don’t forget to use a Remote Event for that.

So this is what i came up with.

**I tested it recently but got - **

Also sorry if my scripting is just sad. im really new at studios and got accepted into the forums recently. Iv been trying my best to fit in but i just trash my reputation :confused:

again sorry if im just stupid…

Screen Shot 2020-10-20 at 18.54.20

What is that “local” doing there. You havent defined anything. It literally gives you a red line.

alright. i defined a couple more things and fixed all of the errors. my only problem now is that the gui timer isnt changing and im unsure of how to connect it, since im using my only known method…


It started on January 1, 1970, or December 31, 1969, depending on what time zone you’re in.

yes but how come it wont display the timer on the gui?

You can try this. It worked for me ( I also use it to make players unlock things).

--=============================================
--=========SCRIPT=============================
--=============================================
local PlayerData = game:GetService("DataStoreService"):GetDataStore("PlayerData")
--Save Data Function
local function saveData(player)
	
	if player:FindFirstChild("ItemsUnlocked") then

		local inventory = {}

		for i, v in pairs (player.ItemsUnlocked:GetChildren()) do
			table.insert(inventory,v.Name)
		end

		local success, errorMessage = pcall(function()
			PlayerData:SetAsync(player.UserId.."ItemsUnlocked",inventory)
		end)

		if success then 
			print("Data Saved")
		else
			print("Error")
		end
	end
	
	if player:FindFirstChild("ItemAwaiting") then
		local success, errorMessage = pcall(function()
			PlayerData:SetAsync(player.UserId.."ItemAwaiting",player.ItemAwaiting.Value)
		end)
	end
	
	if player:FindFirstChild("UnlockWaitTime") then
		local success, errorMessage = pcall(function()
			PlayerData:SetAsync(player.UserId.."UnlockWaitTime",player.UnlockWaitTime.Value)
		end)
	end
	
	if player.ItemAwaiting.Value ~= nil then
		pcall(function()
			local timeval = os.time()
			PlayerData:SetAsync(player.UserId.."LastTimeLeft",timeval)
		end)
	end

end

game.Players.PlayerAdded:Connect(function(player)
	
	local ItemAwaiting = Instance.new("StringValue")
	ItemAwaiting.Name = "ItemAwaiting"
	ItemAwaiting.Parent = player
	
	local ItemWait = Instance.new("IntValue")
	ItemWait.Name = "ItemWaitTime"
	ItemWait.Parent = player
	
	local unlocked = Instance.new("Folder")
	unlocked.Name = "ItemsUnlocked"
	unlocked.Parent = player
	
	
	local savedData	
	pcall(function()
		savedData = PlayerData:GetAsync(player.UserId.."ItemAwaiting")
	end)
	if savedData ~= nil then
		player.ItemAwaiting.Value = savedData
	else
		player.ItemAwaiting.Value = ""
	end
	

	local savedData	
	pcall(function()
		savedData = PlayerData:GetAsync(player.UserId.."ItemWaitTime")
	end)
	if savedData ~= nil then
		player.ItemWaitTime.Value = savedData
	else
		player.ItemWaitTime.Value = nil
	end

	local timenow = os.time()
	local data
	pcall(function()
		data = PlayerData:GetAsync(player.UserId.."LastTimeLeft")
	end)

	if data ~= nil then
		local timegone = timenow - data
		player.ItemWaitTime.Value = player.ItemWaitTime.Value - timegone
	else
		print("NO SPELL")
	end

	
	local ItemsUnlocked = PlayerData:GetAsync(player.UserId.."ItemsUnlocked")
	if ItemsUnlocked then
		for i, item in pairs (ItemsUnlocked)do
			local stringValue = Instance.new("StringValue")
			stringValue.Name = item
			stringValue.Parent = player.ItemsUnlocked
			wait()
		end
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function(player)
	for i,player in pairs(game.Players:GetPlayers()) do
		saveData(player)
	end
end)
--To unlock an item from a client, create a remote event in ReplicatedStorage named UnlockItem. Fire it with the item's name (string value), item's cost(number value), and the time need to unlock it (integer value) in seconds.
game.ReplicatedStorage.UnlockItem.OnServerEvent:Connect(function(player,ItemName,ItemCost,ItemTime)

	if player.leaderstats.Cash.Value >= ItemCost then
		-- if the player has enough cash to unlock this item
		if player.ItemsUnlocked:FindFirstChild(ItemName) ~= nil then
			-- if the player has not unlocked this item yet
			player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - ItemCost
			player.ItemAwaiting.Value = ItemName
			player.ItemWaitTime.Value = ItemTime
		end
	end
end)

while true do
	wait(1)
	for i, v in pairs (game.Players:GetChildren()) do
		if v.ItemAwaiting.Value ~= "" then
			v.ItemTimeWait.Value = v.ItemTimeWait.Value - 1
			if v.ItemTimeWait.Value <= 0 then
				v.ItemTimeWait.Value = nil
				local newItem = Instance.new("StringValue")
				newItem.Parent = v.ItemsUnlocked
				newItem.Name = v.ItemAwaiting.Value
				v.ItemAwaiting.Value = ""
			end
		end
	end
end