So i want to create a part so i have a textbutton that would spawn an part but i don't know how to spawn part for all of the server's? I heard people talking about message system i did watch a video but i don't understand much it will be great if u give me an example if u know anything about it.
script.Parent.MouseButton1Click:Connect(function() local Part = Instance.new("Part") Part.Parent = game.Workspace -- How can i do so if i click everyone in all server will spawn the part in the workspace. end)
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() -- tests the function
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.