Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this multi-server message script work?

Hello, guys! I am back with a question that I can't find an answer to. Here is the code, inside a ServerScript.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AdminMsgEvent = ReplicatedStorage:WaitForChild("AdminMsgEvent")
local DataStoreService = game:GetService("DataStoreService")
local MessageStore = DataStoreService:GetDataStore("DogeAdminMessage")
 
local function onAdminMsgFired(player, txt)
	MessageStore:SetAsync("DogeKey", txt)
end

AdminMsgEvent.OnServerEvent:Connect(onAdminMsgFired)

local connection

function onMessageUpdate(txt)
	local M = Instance.new("Message")
	M.Parent = game.Workspace
	M.Text = txt
	wait(5)
	M:Destroy()
end


connection = MessageStore:OnUpdate("DogeKey", onMessageUpdate)

However, when I do this, it only shows the message for the server I am on (yes, I tested with multiple accounts in different servers,) but doesn't show up on any other servers. It then gives this error:

13:53:33 -- Calbacks cannot yield.

When I try to send another message after that, this error shows up:

13:50:19 -- Request was throttled. Try sending fewer requests. Key = DogeKey

When this happens, the DataStore refuses to do anything, and no matter how long I wait (I even waited an hour!) it still says that. Plus, the message doesn't show up on my server, or any other servers at all.

--Thanks in advance to those of you who helped!

1 answer

Hello TinyScripter!

For your first error: Callbacks cannot yield. This is because you have a wait(5) in your script, which is yielding, and DataStore callbacks cannot yield. You can fix this using a coroutine:

local connection

sendMessage = coroutine.create(function(txt)			-- Creates a new Coroutine.
	local M = Instance.new("Message")
		M.Parent = game.Workspace
		M.Text = txt
		wait(5)
		M:Destroy()
end

function onMessageUpdate(txt)
 	coroutine.resume(sendMessage, txt)				-- Activates the Coroutine.
end

connection = MessageStore:OnUpdate("DogeKey", onMessageUpdate)

For more information on Coroutines, here's a great wiki article.

For your second error: Request was throttled.

This happens because you're most likely sending too many requests to the datastore. Datastores have limits, so you're going to have to setup a loop that checks every few minutes. Here's the link to DataStore limits.

Hope you get your system to work!

This discussion is preserved as it was. New answers, comments, and votes are closed.