How to make a server script fire in 1 server only?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to create a script which sends a JSON message to a Discord WebHook. It is supposed to announce the time left until an Event happens

  1. What is the issue? Include screenshots / videos if possible!

The Script works, but the Script fires multiple times if there are more active servers than 1.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I didn’t try anything yet becuase I don’t know how to fix the issue. I tried adding debounce but that didn’t work

1 Like

You can try to use datastore, an example is when the script is fully loaded on 1 server it sets its status as loaded

When the script tries to fire the JSON code it will request from datastores if the script is loaded already, if it is loaded then it will just return and void the rest of the code on other servers.

1 Like

How can I script that? I’m not really an expert in Data Store Scripting. Can you give me some examples or tips?

this guide should help you out a bit

it has a ton of documentation

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Name")
local messageRate = 60 -- message every 60 seconds

while true do
    -- get the current time
    local osTime = os.time()

    local success, value = pcall(dataStore.UpdateAsync, dataStore, function(value)
        -- you dont need to always do this but the first time to use this key value will = nil
        if value == nil then value = 0 end
        -- if the datastore time has a lower deltatime then messageRate abort the UpdateAsync
        if osTime - value < messageRate then return nil end
        -- if the delta time is grater then messageRate then update the datastore to the current time
        return osTime
    end)
    if the datastore update was successfull and the time was updated
    if success and value ~= nil then
        -- send a message to discord
    end
   -- wait some time before we check then datastore again must wait at least 6 seconds
    task.wait(10)
end

Can you make the script to just make one server Value true? (game.ServerScriptService.Script.messagingEnabled.Value = True)
The messaging gets handled by a countdown script. It should activate the messaging script if the server value is true. So the data store should make a value true in only 1 server.

the code in my previous message will make all servers work as a team to message discord every 60 seconds (discord will only receive 1 massage every 60 seconds)

the code below will make 1 server do all the work and that will be the only server that messages discord (discord will only receive 1 massage every 60 seconds)
if the 1 server shuts down then another random server will take the place of the first server

local memoryStoreService = game:GetService("MemoryStoreService")
local sortedMap = memoryStoreService:GetSortedMap("SortedMap1")
 
local messageRate = 60 -- message every 60 seconds

while true do
	local success, value = pcall(sortedMap.GetAsync,  sortedMap, "Key")
	if success == true then
		if value == nil or value == game.JobId then
			local success, value = pcall(sortedMap.SetAsync,  sortedMap, "Key", game.JobId, messageRate + 1)
			-- send a message to discord
		end
	end
	task.wait(messageRate)
end

out of the 2 methods i sent you i like the first one better it feels more robust