Prepare for a well-made yet lengthy answer
First off…
You’re always welcome to ask for help. Us developers will gladly assist you with the problems you’re having
Now onto your issue. Since you don’t the basics of scripting, below is a thorough tutorial on how you would go on about making a chat tag and how would you make it togglable. It’s lengthy, but you’ll be able to fully understand how you’ll be able to do this
Part 1 - Making the Chat Tag
First thing you’ll need to know about is TextChatService. TextChatService is a service that entirely handles the chat. We will be using TextChatService to add the TextChat to the channels.
Second thing you’ll need to know is Attributes. Attributes are similar to the built-in properties, but they to create and modify your own attributes. We can use attributes to toggle the Chat Tags on and off using the gui.
First, create a Local Script inside the StarterPlayerScripts folder which is located inside the StarterPlayer folder. Name it whatever you’d like. “TextChatHandler” would be a good name.
Next, open the local script and type the following code inside at the top of the script:
local TextChatService = game:GetService("TextChatService")
local Players = game:GetServices("Players")
The Players service basically manages all the Players inside your game. We define our services at the top of our script so we can use them later.
Now, add this to your script:
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
end
Explanation: Above is known as the OnIncomingMessage
callback. The OncomingMessage
callback is called when the Chat is receiving a message from a source. We’ll be using this to detect when a player is chatting and adding the Chat Tag accordingly.
Next, add this following code inside the callback:
local properties = Instance.new("TextChatMessageProperties")
if message.TextSource then
local player = Players:GetPlayerByUserId(message.TextSource.UserId)
if player:GetAttribute("IsVIP") then
properties.PrefixText = "<font color='#F5CD30'>[VIP]</font> " .. message.PrefixText
end
end
return properties
Your script should now look like this:
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
TextChatService.OnIncomingMessage = function(message: TextChatMessage)
local properties = Instance.new("TextChatMessageProperties")
if message.TextSource then
local player = Players:GetPlayerByUserId(message.TextSource.UserId)
if player:GetAttribute("isDev") then
properties.PrefixText = "<font color='#F5CD30'>[Dev]</font> " .. message.PrefixText
end
end
return properties
end
Explanation:
We are creating an Instance called TextChatMessageProperties
that allows us to modify the properties of chat messages. We will be using this to add the Chat Tags.
We then check if message.TextSource
exists. TextSource
is the speaker of the incoming message. And then we get the Player of the speaker by using the GetPlayerByUserId
method. The userId comes from the message TextSource.
Next, we check if the certain attribute a player has is true. In the code above, we check if the player’s isDev
is set to true. (Note: Later in this tutorial, we will create the isDev
attribute and determine when it will be true or false).
Lastly, we modify the message’s properties using rich text to add the Chat tag. Then we returned properties
.
You can now close your local script. We are done with this part.
Part 2 - Handling the Chat Tag
Create a normal script in ServerScriptService and type the following code inside:
local Players = game:GetService("Players")
local developers = {
1234567,
4567890
}
Players.PlayerAdded:Connect(function(player: Player)
end)
Explanation:
Players.PlayerAdded
is an event. Events fire when something happens. In this case, the PlayerAdded
event fires when a player joins the server and passes the player
that joined.
The developers
table contains the User IDs of all the developers, separated by commas. We’ll use this to check if a player is a developer.
Next we’ll if the player that joined’s User ID matches the one in the server. If it does, we will set the playerisDev
attribute to true. If there is no isDev
attribute, setting the attribute automatically creates it. Type the following code inside the PlayerAdded Event:
if table.find(developers, player.UserId) then
player:SetAttribute("isDev", true)
end
Your final script should look like this:
local Players = game:GetService("Players")
local developers = {
1234567,
4567890
}
Players.PlayerAdded:Connect(function(player: Player)
if table.find(developers, player.UserId) then
player:SetAttribute("isDev", true)
end
end)
With this, if a player has the user ID of one that is in the table, they will automatically receive the chat tag. Close this script. On Part 3 will allow us to make the chat tag togglable.
Part 3 - Making the Chat Tag Togglable
Inside your UI, create a local script under the button you want the player to press that toggles the dev chat tag on and off.
Type the following code inside the script:
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
end)
The above code gets the LocalPlayer (the player who is pressing the button). The MouseButton1Click
is an event that fires when the LocalPlayer clicks the button with Mouse Button 1.
Now, all we’ll need to do is set the isDev attribute value to the opposite of what the value of the attribute currently is. If the attribute is set to false, we will set it to true. If the attribute is true, we’ll set it to false.
This can be done with adding 2 lines inside the MouseButton1Click event:
local isDev = LocalPlayer:GetAttribute("isDev")
LocalPlayer:SetAttribute("isDev", not isDev)
Your final code should be this:
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
local isDev = LocalPlayer:GetAttribute("isDev")
LocalPlayer:SetAttribute("isDev", not isDev)
end)
And that’s it. You’re done.
Sorry that this reply was so long. I just wanted it to be thorough so you’ll understand everything since you’re a beginner.
I hope I helped
Let me know if you have any questions or concerns.