How do I change a value throughout all the servers in my game?

So I have this game, that when an hour passes in real-life, its supposed to reroll some values which are randomized, which all need to be the same across every server in my game. How would I do this?

5 Likes

Hi there is 2 ways u can Script that

First u can use data stores and u got a server(Private(or use otherplace)) running that saving random value into data store
and other servers are loading datastores when loadded and when it passes a hour

Sorry for late help

1 Like

how would I carry the data over exactly? sorry I haven’t worked with datasaves much.

1 Like

well imma try script it for u
Okay

1 Like

well u need a server that always on do u have that

or simply u can generate it every day

so you mean another place in the game that always runs?

Yeah kinda or make a web server and the servers get a request

Or u calculate for every hour in every day by math or already calculated

soooooo, how would I keep the server open without any players?

Well u can use

HttpService

aka Getting Data from a website or a host

u can make a host in ur computer or a paid one

Or just use math to calculate

1 Like

You should just make the randomization deterministic. That way you don’t need to communicate between servers: they’ll just calculate the same answers.

Here’s an easy way: you want it to be every hour? Every hour is the N-th hour of the year. This should be the same across all servers, because it’s just based on time. So you calcluate N, and then you use it as the randomseed before doing your rerolls. The cool thing about randomseed is that when you set it, math.random becomes deterministic, so if your first math.random after setting seed is (1,5), this will always give the same answer regardless of server.

local function rerollValues(randomSeed)
	math.randomseed(randomSeed)
	return {math.random(1,100),math.random(1,100), math.random(1,100)}
end

local function newHour()
	
	-- Get the current time in seconds since the epoch
	local currentTime = os.time()

	-- Get the current date and time
	local currentDate = os.date("*t", currentTime)

	-- Calculate the total hours that have passed this year
	local totalHours = (currentDate.yday - 1) * 24 + currentDate.hour
	
	print("Hours index: " .. totalHours)
	
	print(rerollValues(totalHours))
end

So now the randomization is based on the hour, which will be the same in all servers, so they will independently get the same values!

2 Likes

if the server were to shut down or if it was a new server, would it still stay the same? if not then how would I detect other running servers values and make them those ones?

There is a tutorial for a system like this which I used. Search daily shop on devforum. It uses seeds based on the currenttime to generate the same outcome across all servers.

1 Like

Yes, it would stay the same.

-- Get the current time in seconds since the epoch
	local currentTime = os.time()

	-- Get the current date and time
	local currentDate = os.date("*t", currentTime)

	-- Calculate the total hours that have passed this year
	local totalHours = (currentDate.yday - 1) * 24 + currentDate.hour

The current time in seconds since the epoch will always be the same. The start of the ‘epoch’, AKA Unix time, is january 1st 1970. os.time() will return the current time in seconds, not counting leap seconds, since that moment. So if you query it at the same time on 2 servers, it’ll get you the same result.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.