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)