I want to achieve a global timer system that is the same in all servers.
You can do tasks in the game or pay robux to decrease or increase the amount of time on the timer. However, when a player increases/decreases time on a server, only that server is affected, meaning every other running server is not affected by that player’s increase/decrease on time UNTIL the server gets restarted.
I have been stuck at a brick wall. I have tried using remote events, and bindable events, both failing. I’ve found this post— Hourly Reset Timer that is global - Help and Feedback / Scripting Support - Developer Forum | Roblox— with a similar problem, however it resets hourly, and there is no system that allows for time adjustment, so you can use the os.date() function.
local MessagingService = game:GetService("MessagingService")
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local countdownTime = 1000000
local eventName = "GlobalEvent"
local countdownStore = DataStoreService:GetDataStore("GlobalCountdown")
local adjustedTimeStore = DataStoreService:GetDataStore("AdjustedTimeStore")
local countdownValue
local countdownEndTime
local adjustTimeEvent = game.ReplicatedStorage.adjustTimeEvent
local Template = Instance.new("BoolValue")
Template.Name = "leaderstats"
local AdjustedTimeValue = Instance.new("IntValue", Template)
AdjustedTimeValue.Name = "AdjustedTime"
local function loadAdjustedTime(player)
local success, data = pcall(function()
return adjustedTimeStore:GetAsync(player.UserId)
end)
if success and data then
player.leaderstats.AdjustedTime.Value = data
else
player.leaderstats.AdjustedTime.Value = 0
end
end
local function saveAdjustedTime(player)
local success, errorMessage = pcall(function()
adjustedTimeStore:SetAsync(player.UserId, player.leaderstats.AdjustedTime.Value)
end)
if not success then
warn("Failed to save adjusted time for " .. player.Name .. ": " .. errorMessage)
end
end
Players.PlayerAdded:Connect(function(player)
local stats = Template:Clone()
stats.Parent = player
loadAdjustedTime(player)
end)
Players.PlayerRemoving:Connect(function(player)
saveAdjustedTime(player)
end)
local function initializeCountdown()
local success, savedTime = pcall(function()
return countdownStore:GetAsync("CountdownEndTime")
end)
if success and savedTime then
countdownEndTime = savedTime
else
countdownEndTime = os.time() + countdownTime
pcall(function()
countdownStore:SetAsync("CountdownEndTime", countdownEndTime)
end)
end
end
initializeCountdown()
local function updateCountdown()
while true do
countdownValue = countdownEndTime - os.time()
if countdownValue <= 0 then
MessagingService:PublishAsync(eventName, "CountdownEnded")
countdownEndTime = os.time() + countdownTime
pcall(function()
countdownStore:SetAsync("CountdownEndTime", countdownEndTime)
end)
end
wait(1)
end
end
spawn(updateCountdown)
MessagingService:SubscribeAsync(eventName, function(message)
print("Global event triggered!")
end)
local CountdownEvent = game.ReplicatedStorage.CountdownEvent
local function broadcastCountdown()
while true do
CountdownEvent:FireAllClients(countdownValue)
wait(1)
end
end
spawn(broadcastCountdown)
local function adjustTime(player, seconds)
countdownEndTime = countdownEndTime + seconds
pcall(function()
countdownStore:SetAsync("CountdownEndTime", countdownEndTime)
end)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local adjustedTime = leaderstats:FindFirstChild("AdjustedTime")
if adjustedTime then
adjustedTime.Value = adjustedTime.Value + math.abs(seconds)
end
end
end
adjustTimeEvent.Event:Connect(function(player, seconds)
adjustTime(player, seconds)
end)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
saveAdjustedTime(player)
end
end)
Does anyone have any ideas? Note : It is a little hard to provide a video or image/screenshot since I need to run 2 servers at a time.