How do I save the progress of a countdown timer when a player leaves?

I have a prize-wheel system in my game, a player can spin the wheel, and once the spin ends there begins a 15 minute cooldown. I want this cooldown to persist even if the player leaves, so that when they join back the timer will have gone down by the amount of seconds that have passed in real time.

I do not understand how to make DataStore do this, I’ve tried making DataStores but I do not know how to connect the os.time() SaveData to the actual textlabel that displays the time.

Please help
Here’s the section of the prize-wheel script that I need to somehow connect to a DataStore

		local TimeBoard = game.Workspace.Lobby.Timer.SurfaceGui.TimerLabel
		for i = 900, 0, -1 do
			TimeBoard.Text = "Time until next spin " ..i
			wait(1)
		end
		game.Workspace.placeholder.ClickDetector.Parent = game.Workspace.PrizeWheel.Wheel
		debounce = false
end
1 Like

Make a Number value inside of the player called “PrizeWeelCooldown”
Make the PrizeWeelCoolDown value equal to i.
So like this

local TimeBoard = game.Workspace.Lobby.Timer.SurfaceGui.TimerLabel
		for i = 900, 0, -1 do
                        PrizeWeelCooldown.Value = i
			TimeBoard.Text = "Time until next spin " ..i
			wait(1)
		end
		game.Workspace.placeholder.ClickDetector.Parent = game.Workspace.PrizeWheel.Wheel
		debounce = false
end

then what you would do is save it (I’m on mobile right now so I can’t write the code But I’ll give you a tutoriall)

1 Like

When I create the NumberValue, do I create it using a script? or do I put it inside StarterPlayer?

Please wait, I’m writing you a tutorial about it…

Alright, I’ll give you a tutorial about it

  • First of we have to make a value inside the Player for it.
  • Second we have to save the value.

Let’s start with the First part

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

local PrizeWeelCooldown = Instance.new("IntValue")
PrizeWeelCooldown.Value = 0
PrizeWeelCooldown.Parent = Player
end
  • Now we are done with the Value we have to make it equal to the time to the Prize.
local TimeBoard = game.Workspace.Lobby.Timer.SurfaceGui.TimerLabel
		for i = 900, 0, -1 do
                        PrizeWeelCooldown.Value = i
			TimeBoard.Text = "Time until next spin " ..i
			wait(1)
		end
		game.Workspace.placeholder.ClickDetector.Parent = game.Workspace.PrizeWheel.Wheel
		debounce = false
end
  • Now we need to save it Using datastore
    This should be the full Datastore script.
local DataStoreService = game:GetService("DataStoreService")
local PrizeDataStore = DataStoreService:GetDataStore("PrizeDataStore")




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

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

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

	if PrizeData then
		PrizeWeelCooldown.Value = PrizeData
	else
		PrizeWeelCooldown.Value = 0
	end
end)


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

	local Success, Errormessage = pcall(function()

		local PlayerUserId = "Player_" .. Player.UserId

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

	end)

	if not Success then
		print(Errormessage)
	end
end)

Did it work? if so Please mark this as a solution if it didn’t please tell me the error code also make sure to test it in the main game (not studio).

1 Like

I have an error in this part

its saying that PrizeWheelCooldown = nil?

local TimeBoard = game.Workspace.Lobby.Timer.SurfaceGui.TimerLabel
		for i = 900, 0, -1 do
                        PrizeWeelCooldown.Value = i
			TimeBoard.Text = "Time until next spin " ..i
			wait(1)
		end
		game.Workspace.placeholder.ClickDetector.Parent = game.Workspace.PrizeWheel.Wheel
		debounce = false
end

Ok, I see is your code a local script or server script?

The prize-wheel script is a Local script

Try this

local Player = game.Players.LocalPlayer 
		for i = 900, 0, -1 do
                        Player.PrizeWeelCooldown.Value = i
			TimeBoard.Text = "Time until next spin " ..i
			wait(1)
		end
		game.Workspace.placeholder.ClickDetector.Parent = game.Workspace.PrizeWheel.Wheel
		debounce = false
end

If it doesn’t save then you will have to use a RemoteEvent.

Ok it fixed the other error, but now it says "PrizeWeelCooldown is not a valid member of Player “Players.stockeye”

What is the PrizeWeelCoolDown parented to?
BTW don’t do what I did up, It’s wrong

Ok, Looks like it won’t save since this is client (Just remove all the code and paste those).

Add a Folder called RemoteEvents in ReplicatedStorage
then Add a RemoteEvent named “PrizeEvent”

Then add this code

local Player = game.Players.LocalPlayer 
		for i = 900, 0, -1 do
                        game.ReplicatedStorage.RemoteEvents.PrizeEvent:FireServer(i)
			TimeBoard.Text = "Time until next spin " ..i
			wait(1)
		end
		game.Workspace.placeholder.ClickDetector.Parent = game.Workspace.PrizeWheel.Wheel
		debounce = false
end

Now let’s get back to our Server Script.
Add this code

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

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

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

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


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

	local Success, Errormessage = pcall(function()

		local PlayerUserId = "Player_" .. Player.UserId

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

	end)

	if not Success then
		print(Errormessage)
	end
end)
1 Like

It seems to be working perfectly until I leave the game. It starts to countdown, but then the output keeps repeating an error. It says "PrizeWeelCooldown is not a valid member of Player “Players.stockeye”

Everything else works without a problem though, and I made sure to fix any typos. Not sure why it’s doing that

Oh We forgot to name it, Try this.
Also don’t forgot to make it when the PrizeWeelCooldown value reaches 0 it should give the player a spin and the time text should be equal to the PrizeWeelCooldown value.

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

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

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

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


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

	local Success, Errormessage = pcall(function()

		local PlayerUserId = "Player_" .. Player.UserId

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

	end)

	if not Success then
		print(Errormessage)
	end
end)
1 Like

Awesome, it worked. Appreciate the help!

1 Like