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

How would you run a script in every server of the game?

Asked by 4 years ago

How would you run a script in every server of the game? I've tried using data-stores but I have no idea how.

For example, a button in the game that would shout a string in every server. I am very new to scripting and this website, so please explain it in terms a noob can understand.

3 answers

Log in to vote
1
Answered by
fff054 51
4 years ago

So, I Think That What You Are Looking For Is Actually Called Messaging Server.

Roblox Studio Wiki: https://developer.roblox.com/en-us/articles/cross-server-messaging

This Feature Allows You To Communicate Through Each Server That Is Currently Running. If You Want To Announce When A Player Joins The Game In Each Server, It Would Probably Look Like This:

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")

local Topic = "Announcement" -- Write The Topic Of Your Message. This Helps You Stay Organized

Players.PlayerAdded:Connect(function(player) -- Triggers When A New Player Joins.
    local publishSuccess, publishResult = pcall(function() 
        local message = player.Name.." Has Joined The Game!" -- The Data That It's Going To Be Sent.
        MessagingService:PublishAsync(Topic, message) -- Sends The Data With The Topic.
    end)
end)

Now, What Comes Next Is The Server Reaction (How The Server Will React To This Data). First, You Need To "Subscribe" All The Servers To Your Message. Here's How (This Code Goes In The Same Script By The Way):

Players.PlayerAdded:Connect(function(player) 
    local subscribeSuccess, subscribeConnection = pcall(function()
        return MessagingService:SubscribeAsync(Topic, function(message)
            local Hint = Instance.new("Hint", workspace) -- Creates A Little Bar In Which You Can Write Text.
            Hint.Text = message.Data -- Puts The Message Into That Bar
            game:GetService("Debris"):AddItem(Hint, 8) -- Deletes The Bar After 8 Seconds.
        end)
    end)
    if subscribeSuccess then
        player.AncestryChanged:Connect(function()
            subscribeConnection:Disconnect()
        end)
    end
end)

I Hope This Helped You. If You Have Any Doubts, You Can Check On The Link That Is At The Beginning Or Watch Some Tutorials. Good Luck Learning!

Ad
Log in to vote
0
Answered by 4 years ago

IMO, the best way to do this is to host a free website or webserver. There's many websites that let you do this, with glitch.com being the best. You can even set it up to be 24/7. However, you could make a script that fetches a datastore every x amount of seconds/minutes, and checks if there's a string to be shouted. If so, shout it, and set that value to a blank string again. This does however can cause some missing shouts if people shout at similar times, so you could maybe tweak this idea a bit to make it perfectly suitable for you you're trying to do.

0
Thank you. I'll keep that in mind. votedOff 10 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

ummm u could get creative with Datastore by having each server constantly check for when the value of a datastore key was changed, or you could us the HTTP service, but that would require you to have an external server..

the following code could be used when reading data from different servers

local HTTPService  =  game:GetService("HttpService");
local DatastoreService = game:GetService("DataStoreService");
local datastore = DatastoreService:GetDataStore("CHATSISGDHU");

local last_updated  = nil;
function check_update_status()
    local data = HTTPService:JSONDecode(datastore:GetAsync("inter_server_chat") or "{}");

    local last_updated_from_ds = data.LastUpdated;

    if(last_updated_from_ds ~= last_updated and last_updated_from_ds) then
        print("new msg:",data.Message);
    last_updated = last_updated_from_ds
    end
end

while wait(3) do
    local success, err = pcall(check_update_status);
end

the following could be used when writting the data; let say you want to shout a message to all servers when you chat \shout ~message here~ you could do it like this:

local Players = game.Players
local HTTPService  =  game:GetService("HttpService");
local DatastoreService = game:GetService("DataStoreService");
local datastore = DatastoreService:GetDataStore("CHATSISGDHU");

local admins = {"void_node","VotedOff"}
Players.PlayerAdded:Connect(function(player)
    for _, admin in pairs(admins) do
        if(admin:lower() == player.Name:lower()) then
            player.Chatted:Connect(OnChat)
        break
        end
    end
end)

function OnChat(msg)
    local msg_shouting = msg:match("^\\shout%s-(.+)");
    if(msg_shouting) then
        shout_to_servers(msg_shouting)
    end
end

function shout_to_servers(msg)
    datastore:UpdateAsync("inter_server_chat", function(current_data)
        local new_data = {
            LastUpdated = os.time();
            Message = msg;
        }
        return HTTPService:JSONEncode(new_data);
    end)
end

this should hopefully work.

Answer this question