Show NPC messages
You can write your own scripts that display any message you want, sent by anyone you want. You can use that to display announcements, but most of the time it's used to display messages "sent" by NPCs.
In this tutorial, we will create a button that will display a message sent by an Announcer
once clicked. We assume you have already inserted the Host GUI into your game (into ServerScriptService
). If not, you can see how to do that in the installation tutorial.
We do not filter the messages you put in your scripts. You are responsible for keeping them compliant with Roblox terms.
To begin, add a part to your game.
Now add a Script
and a ClickDetector
to that part by selecting the +
button next to that part in the explorer and adding those things.
When you add a script, a script editor window should get opened. To be able to display custom messages on the Host GUI, we need to import the Host GUI's API script. This needs to be done in every script where you plan to use the Host GUI, but only once per script. Include this line at the top of your script:
local HostGUI = require(game:GetService("ServerScriptService").HostGui.Scripts.Api)
Now we need to listen for when the button gets pressed. This is done in the same way as in every other script:
local HostGUI = require(game:GetService("ServerScriptService").HostGui.Scripts.Api)
script.Parent.ClickDetector.MouseClick:Connect(function()
-- we will add more code here
end)
Once the button gets clicked, we want to display a message on the GUI. To do that, we will use the :SendMessage
function of the API. We will need to provide a username and a message
local HostGUI = require(game:GetService("ServerScriptService").HostGui.Scripts.Api)
script.Parent.ClickDetector.MouseClick:Connect(function()
HostGUI:SendMessage("Announcer", "This is a very important announcement from the Announcer")
end)
This is all you need to display an NPC message. When you start the game, you should be able to press the button and see the message appear.