Hello, by counting with a simple counter, a new value is entered when the counter is finished. That value is saves in the server’s datastore. But it records different values in 2 servers.
What I want is I want the values on more than one server to be the same when the counter runs out.
How can I do this?
Server Script
local ds = game:GetService("DataStoreService"):GetDataStore("GlobalValue")
local SERVER_KEY_NAME = "Server"
local success, data = pcall(function()
return ds:GetAsync(SERVER_KEY_NAME)
end)
if success then
print(data)
end
local AllEvent = game.ReplicatedStorage.AllEvent
local GetValue = game.ReplicatedStorage.getValue
GetValue.OnServerInvoke = function(player)
if success then
return data
end
end
local CountMin =1
local function generateRandomString()
local randomLength = math.random(4, 10)
local chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
local result = ""
for i = 1, randomLength do
local randIndex = math.random(1, #chars)
result = result .. string.sub(chars, randIndex, randIndex)
end
return result
end
local function getRemainingSeconds()
local Secound = CountMin*60
local unixTime = os.time()
return Secound - (unixTime % Secound)
end
local lastSecond = -1
while true do
local remaining = getRemainingSeconds()
if remaining ~= lastSecond then
lastSecond = remaining
print("Count Server:", remaining)
if remaining == CountMin*60 then
local randomValue = generateRandomString()
print("New Value:", randomValue)
ds:SetAsync(SERVER_KEY_NAME,randomValue)
local success1, data1 = pcall(function()
return ds:GetAsync(SERVER_KEY_NAME)
end)
if success1 then
AllEvent:FireAllClients(data1)
end
end
end
wait(0.99)
end
Client Script
local Event = game:GetService("ReplicatedStorage"):WaitForChild("AllEvent")
local Event2 = game:GetService("ReplicatedStorage"):WaitForChild("getValue")
local GetValueNewJoined = Event2:InvokeServer()
if GetValueNewJoined then
script.Parent.TextLabel.Text = "Value: "..GetValueNewJoined
end
Event.OnClientEvent:Connect(function(value)
script.Parent.TextLabel.Text = "Value: "..value
end)
local function formatTime(seconds)
local minutes = math.floor(seconds / 60)
local remainingSeconds = seconds % 60
return string.format("%02d:%02d", minutes, remainingSeconds)
end
local CountMin = 1
local function getRemainingSeconds()
local Secound = CountMin*60
local unixTime = os.time()
return Secound - (unixTime % Secound)
end
local lastSecond = -1
while true do
local remaining = getRemainingSeconds()
lastSecond = remaining
print("Count Client:", remaining)
script.Parent.Count.Text = formatTime(remaining)
wait(0.99)
end