Holding the same value on all servers - Help

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
2 Likes

I don’t think you need datastores here. Use Random objects as your way of having the value generation be synced across servers. Having Random objects with the same seed parameter will produce the same random values.

I’ll use os.time() as an example for your seed.

rng = Random.new(os.time())

Then you could generate your value length and value like this:

-- You can think of NextInteger the same way as your math.randoms, just with set RNG.
local randomLength = rng:NextInteger(4, 10)
...
local randIndex = Random.new(os.time() * i):NextInteger(1, #chars)

You might want to tweak how you make the new Random objects in the iterable, but it should work either way.

Edit: Added some bits since I thought it wasn’t explaining enough.

1 Like

Thank you. I didn’t know Random. I learned new.

1 Like