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 5 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
5 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:

01local MessagingService = game:GetService("MessagingService")
02local Players = game:GetService("Players")
03 
04local Topic = "Announcement" -- Write The Topic Of Your Message. This Helps You Stay Organized
05 
06Players.PlayerAdded:Connect(function(player) -- Triggers When A New Player Joins.
07    local publishSuccess, publishResult = pcall(function()
08        local message = player.Name.." Has Joined The Game!" -- The Data That It's Going To Be Sent.
09        MessagingService:PublishAsync(Topic, message) -- Sends The Data With The Topic.
10    end)
11end)

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):

01Players.PlayerAdded:Connect(function(player)
02    local subscribeSuccess, subscribeConnection = pcall(function()
03        return MessagingService:SubscribeAsync(Topic, function(message)
04            local Hint = Instance.new("Hint", workspace) -- Creates A Little Bar In Which You Can Write Text.
05            Hint.Text = message.Data -- Puts The Message Into That Bar
06            game:GetService("Debris"):AddItem(Hint, 8) -- Deletes The Bar After 8 Seconds.
07        end)
08    end)
09    if subscribeSuccess then
10        player.AncestryChanged:Connect(function()
11            subscribeConnection:Disconnect()
12        end)
13    end
14end)

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 5 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 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 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

01local HTTPService  =  game:GetService("HttpService");
02local DatastoreService = game:GetService("DataStoreService");
03local datastore = DatastoreService:GetDataStore("CHATSISGDHU");
04 
05local last_updated  = nil;
06function check_update_status()
07    local data = HTTPService:JSONDecode(datastore:GetAsync("inter_server_chat") or "{}");
08 
09    local last_updated_from_ds = data.LastUpdated;
10 
11    if(last_updated_from_ds ~= last_updated and last_updated_from_ds) then
12        print("new msg:",data.Message);
13    last_updated = last_updated_from_ds
14    end
15end
16 
17while wait(3) do
18    local success, err = pcall(check_update_status);
19end

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:

01local Players = game.Players
02local HTTPService  =  game:GetService("HttpService");
03local DatastoreService = game:GetService("DataStoreService");
04local datastore = DatastoreService:GetDataStore("CHATSISGDHU");
05 
06local admins = {"void_node","VotedOff"}
07Players.PlayerAdded:Connect(function(player)
08    for _, admin in pairs(admins) do
09        if(admin:lower() == player.Name:lower()) then
10            player.Chatted:Connect(OnChat)
11        break
12        end
13    end
14end)
15 
View all 31 lines...

this should hopefully work.

Answer this question