How would I run a script on only one server of my game?

Hi, I’ve recently been struggling with this issue where I would like to use only one server to run certain logic.

The way I’ve been going about it currently is assigning one server through a process, and then through the messaging service letting the other servers know that there’s already an assigned server. However this system is flawed since it has shown not to work, either because of the messaging delay, or exceeding the server’s limits.

Is there any solution to this problem?

Well only one server? hmm if I’m reading this properly then I guess something id do is check the max mount of players in the server and see if the current amount of players is <= that. Then add a kick part of the script. That’s probably the easiest way id think?

No I mean lets say I have 10 servers running, then only 1 server out of those 10 should run a piece of code. So basically a universe script

You can try a datastore. When it opens it checks if a datastore value exists, if it does it won’t run.

I tried this, but the problem is that the code that I’m trying to run depends on the time, so for example it only runs at exactly 12:00. Then the problem arises that all of these servers try to access the datastore at the same time and you get unreliable results.

Cross-Server Messaging (roblox.com)

This might help, IDK if it actually will I just did a quick search and skimmed it.

This is what I’ve been using, but from what I can tell it’s not reliable enough, but it might be the fault of the logic I wrote

@ComplicatedParadigm, Ye, I did the same :). According to the purpose we can imagine some virtual tags and notify other sevices about them. So only one server will do stuff if he has this tag.

Would you mind sharing the code you wrote for this purpose? If you’d like it to remain private that’s completely fine too

From the context I didn’t get the way you choosing a server to do something. So, one variant is random finding. In this case we have all services with a script, colculating a random number in a huge range. Services should notify each others about their values and according results we can track the server with minimum random number. But it is optional. You can make another random algorithm if you want to.
Btw, I only got an idea, without the code, and I have never heard about cross-server massaging, due to I didn’t have to use it.

This is a good idea, but it would require servers to all message each other with their number. So if there’s 1000 servers, thats 1000*1000 messages no? Is there a way we could lower that

Ohh or are you saying we store it in datastore

I searched more and find JobID property witch is unic for every server and also it belongs to dataModel or game. You can read about it on DevHub. So in fact, we can use it.
What about speed, we can make it O(n) if massage service can notify neigbour server or something else. We have a variable that store minimum value (I guess it can be jobID), in original this varialbe should contain first server’s jobID. Then current server call another server and send variable with minium digit. Called server should compare varialbe’s value with its own jobID and if it is less, change minimum variable and send the next server variable. At the end we nned to get back to the server witch jobID containing in the variable and do stuff.
This system has O(n) speed - good result.
Btw, sorry for broken english. It isn’t my native language.