This is actually not as hard as you would think. You would need to utilize a service called MessagingService
This service consists of two main functions:
:PublishAsync("Topic")
Sends a message to all the servers that are subscribed to the topic specified in the paramaters. A topic can be anything you want as long as it is a string. I recommend naming the topic something that relates to what you are doing, for organization purposes. In the example above, the topic is called "Topic".
:SubscribeAsync("Topic", function() end)
Listens for any publishes made to a specific topic, and then runs a function. The topic must be the same as the topic you publish to.
One thing to keep in mind is that these methods CAN FAIL so you should use pcalls and handle errors accordingly.
Now that you understand how messaging service works, we can get coding. Keep in mind that you can only test this alone in studio (creating multiple servers in studio won't work either). If you want to test it across servers then you need to publish the game and play it using the actual ROBLOX client. If you want to test it alone in studio you need to publish the game, press file in the top left corner of ROBLOX Studio, press game settings, navigate over to Security and then enable Studio API Services. Make sure to press save changes.
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local TOPIC = "CreatePart"
function createPart(position, size)
local newPart = Instance.new("Part")
newPart.Position = Vector3.new(position[1], position[2], position[3])
newPart.Parent = workspace
newPart.Name = "PartoPart"
newPart.Size = Vector3.new(size[1], size[2], size[3])
newPart.Anchored = true
end
Players.PlayerAdded:Connect(function(player)
local connection
local subscribeSuccess, subscribeResult = pcall(function()
connection = MessagingService:SubscribeAsync(TOPIC, function(payload)
local data = payload.Data
createPart(data.Position, data.Size)
end)
end)
Players.PlayerRemoving:Connect(function(p)
if p.UserId == player.UserId then
connection:Disconnect()
end
end)
end)
function spawnGlobalPart()
local partPositionAndSize = {
Position = {0,5,0};
Size = {1,1,1}
}
local publishSuccess, publishResult = pcall(function()
MessagingService:PublishAsync(TOPIC, partPositionAndSize)
end)
if publishSuccess then
print("Part globally spawned")
else
warn(publishResult)
end
end
wait(5)
spawnGlobalPart()
And with that finished, you've got yourself a script that will add a part inside all your servers when the spawnGlobalPart() function is ran.
FAQ:
What is connection and why is it equal to a function?
The function SubscribeAsync() returns a RBLXScriptSignal. An RBLXScriptSignal can be disconnected to essentially stop it from listening for events. By disconnecting it when a player leaves the game you are closing any open connections to prevent exceeding the MessagingService limitations.
Why do you do payload.Data to reference the data you sent through MessagingService?
*This is because when you send data to a topic it is actually wrapped in an array which has two indexes. The first being Data, which contains all the data you send using PublishAsync(). The second index is Sent, which is the os.time() the data was published at. So in our case, payload would look like this:
payload = {
Data = {
Position = {0,5,0};
Size = {1,1,1}
};
Sent = os.time()
}
Make sure to upvote this post if it helped. I am grinding discord ranks right now lmao.
2 comments
Look to https://developer.roblox.com/en-us/api-reference/class/MessagingService it should solve your problem
yes i saw this but i don't know how to do it i even saw a video but i don't know what to type there.