Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make announcement system with chat command?

Hello! I need some help with my announcement system. How do i make GUI text change with a chat command? e.g.

/a Visit Scriptinghelpers.org! then that text goes on a startersGUI for everyone to see. I'll also work with a group rank so only admins can change it.

If you could help me that would be super nice! You can add credit if you want to.

Many thanks, Milan - Leaning scripter

2 answers

First, you'll need to read a .Chatted event by using Player.Chatted

--ServerScript
game.Players.PlayerAdded:Connect(function(player)
	-- You can make an If statement for the player here 
	player.Chatted:Connect(function(msg)
		
	end)
end)

Then, you'll need to use string.Sub to determine whether the player said "/a" at the begininng and to get the text after that.

-- ServerScript
game.Players.PlayerAdded:Connect(function(player)
	-- You can make an If statement for the player here 
	player.Chatted:Connect(function(msg)
		if string.sub(msg,1,3) == "/a " then -- this checks whether the first 3 characters include a "/a " including the space.
			local announcementText = string.sub(msg,4) -- Any text after the space bar would be inside this variable.
			
		end
	end)
end)

Now, you can use Remote Events to talk to the client. For this, you'll need to put a RemoteEvent in ReplicatedStorage.

-- Full Server Script
game.Players.PlayerAdded:Connect(function(player)
	-- You can make an If statement for the player here 
	player.Chatted:Connect(function(msg)
		if string.sub(msg,1,3) == "/a " then -- this checks whether the first 3 characters include a "/a " including the space.
			local announcementText = string.sub(msg,4) -- Any text after the space bar would be inside this variable.
			game.ReplicatedStorage["RemoteEventName"]:FireAllClients(announcementText)
		end
	end)
end)

Now, you'll need a LocalScript inside the Gui for when it recieves the call from the server.

-- Local Script
game.ReplicatedStorage["RemoteEventName"].OnClientEvent:Connect(function(msg)
	local textLabel = script.Parent.TextLabel -- Put the path for the textlabel here.
	textLabel.Text = msg
end)

Or you can set the text into a StringValue and the TextLabel can always set its text to the value.

2 comments

0

Thank you! It works!

0

The third argument of string.sub() is exclusive, therefore the space will not be accounted for.

Cross-server announcement support using MessagingService in correlation to the post above:

-------------------------< ServerScriptService: Script >-------------------------
local MessagingService = game:GetService("MessagingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Chat = game:GetService("Chat")

local Announced = ReplicatedStorage:WaitForChild("Announced") --// RemoteEvent
local Subscription = "GlobalAnnounce"

local GroupId = --// Group ID
local AdminId = --// Rank ID

local Prefix, Call = ';', "announce"

Players.PlayerAdded:Connect(function(Player)
    if (Player:GetRankInGroup(GroupId) == AdminId) then
       Player.Chatted:Connect(function(ChatMessage, Recipient)
           if (Recipient) then return end
           if (ChatMessage:sub(1,#(Prefix..Call)):lower() == (Prefix..Call)) then
               local Announcement = ChatMessage:sub((#Prefix..Call)+2)
               local Filter = Chat:FilterStringForBroadcast(Announcement, Player)
               MessagingService:PublishAsync(Subscription, Filter)
           end
        end)
    end
end)

MessagingService:SubscribeAsync(Subscription, function(Announcement)
   Announced:FireAllClients(Announcement.Data)
end)

-------------------------< StarterGui: LocalScript >-------------------------

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

local Announced = ReplicatedStorage:WaitForChild("Announced")

local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local AnnouncementGui = PlayerGui:WaitForChild("AnnouncementGui")
local Announcement = AnnouncementGui:WaitForChild("Announce")

local function ShowAnnouncement(Data)
    if (Announcement.Text ~= Data) then
        Announcement.Text = Data
    end
end

Announced.OnClientEvent:Connect(ShowAnnouncement)

This discussion is preserved as it was. New answers, comments, and votes are closed.