Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by
Eqicness 255 Moderation Voter
6 years ago

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!

Ad

Answer this question