How do I make my IntValue store how long the player has been gone from the game?

There’s a Prize Wheel in my game that lets you spin it once every 15 minutes (900 seconds).

I have a script right now thats supposed to use a DataStore to save the number of seconds left until the Cooldown ends, but I have no idea how to make it subtract the amount of seconds that have elapsed since the player has begun their cooldown. (Particularly the seconds that occur when the player is out of the game).

The script basically receives a remote event that the prize wheel itself fires, and then sets the cooldown IntValue to the countdown that happens within a local script in my Prize Wheel.

How do I make this DataStore make the countdown continue or end if a player is gone from the game at any point during their cooldown?

local DataStoreService = game:GetService("DataStoreService")
local PrizeDataStore = DataStoreService:GetDataStore("PrizeDataStore")




game.Players.PlayerAdded:Connect(function(Player)

	local PrizeWheelCooldown = Instance.new("IntValue")
	PrizeWheelCooldown.Name = "PrizeWheelCooldown"
	PrizeWheelCooldown.Value = 0
	PrizeWheelCooldown.Parent = Player

	local PlayerUserId = "Player_" .. Player.UserId
	local PrizeData = PrizeDataStore:GetAsync(PlayerUserId)

	if PrizeData then
		PrizeWheelCooldown.Value = PrizeData
	else
		PrizeWheelCooldown.Value = 0
	end
end)
game.ReplicatedStorage.RemoteEvents.PrizeEvent.OnServerEvent:Connect(function(Player, i)
	Player.PrizeWheelCooldown.Value = i
end)


game.Players.PlayerRemoving:Connect(function(Player)

	local Success, Errormessage = pcall(function()

		local PlayerUserId = "Player_" .. Player.UserId

		PrizeDataStore:SetAsync(PlayerUserId, Player.PrizeWheelCooldown.Value)

	end)

	if not Success then
		print(Errormessage)
	end
end)

game:BindToClose(function()
	wait(2)
end)
1 Like

This is what I would reccomend to make a timed reward system:

When a player claims their reward save os.time (the current time) to a key with their userId.

To check if a player can claim their reward check if the current time - the saved time is greater than the cooldown time.

To get how much time they have left before the next claim do cooldown - (current time - time of last claim)

2 Likes