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
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)
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).
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
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, 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)
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)