Chat
The Chat service provides functionality for creating and managing chat channels to dictate how players communicate with each other, as well as how receiving messages work in the game.
Properties
All chat channels that exist in the game.
All chat channels that the player has joined.
An event that is fired when a client receives a text message. If this event has no connected listeners, the text message will display normally.
If you connect to this event to do custom messaging logic, use the DisplayTextMessage
method to send the final results.
Class.ChannelInfo, Class.Player (the sender), and string are passed as the parameters.
Chat.TextMessageReceivedHandler:Connect(function(channel, player, message)
Chat:DisplayTextMessage(channel, player, message)
end)
An event that is fired when a client receives a whisper. If this event has no connected listeners, the whisper will display normally.
If your game does not allow whispering at certain times or only with certain people, this event should be connected to and use the DisplayTextMessage
method only when whispering is allowed.
Class.ChannelInfo, Class.Player (the sender), and string are passed as the parameters.
local canWhisper = false
Chat.WhisperReceivedHandler:Connect(function(channel, player, message)
if canWhisper then
Chat:DisplayTextMessage(channel, player, message)
end
end)
An event that is fired by the server to all clients in the same channel as the player that joined. Class.ChannelInfo and Class.Player are passed as the parameters.
Chat.PlayerJoinedChannel:Connect(function(channel, player)
Chat.TextMessageReceivedHandler:Connect(function(channel, player, message)
if message == "Hello" then
Chat:DisplayTextMessage(channel, player, "Hello, " .. player.name .. "!")
end
end)
end)
An event that is fired by the server to all clients in the same channel as the player that left. Class.ChannelInfo and Class.Player are passed as the parameters.
Chat.PlayerLeftChannel:Connect(function(channel, player)
print(player.name .. " has left the channel.")
end)
An event that is fired by the server to all clients when a channel is destroyed. Class.ChannelInfo is passed as the parameter.
Chat.ChannelDestroyed:Connect(function(channel)
print("Channel " .. channel.name .. " has been destroyed.")
end)
Methods
Creates a new channel. If the channel already exists, it will return the existing channel.
local isGameRunning = true
local ghostChannel = Chat:CreateChannel("Ghost", true, false)
local generalChannel = Chat:CreateChannel("General", true, true)
server.PlayerConnected:Connect(function(player)
if isGameRunning then
Chat:AddPlayerToChannel(generalChannel, player) -- General channel is for players who are in the game
else
Chat:AddPlayerToChannel(ghostChannel, player) -- Ghost channel is for players who are not in the game
end
end)
Parameters
channelName
string
allowText
boolean
allowVoice
boolean
Returns
Destroys the given channel, removing all players from it.
local generalChannel = Chat:CreateChannel("General", true, true)
server.PlayerConnected:Connect(function(player)
Chat:AddPlayerToChannel(generalChannel, player)
end)
server.PlayerDisconnected:Connect(function(player)
Chat:RemovePlayerFromChannel(generalChannel, player)
end)
server.PlayerLeft:Connect(function(player)
Chat:DestroyChannel(generalChannel)
end)
Parameters
channel
ChannelInfoReturns
void
Adds the given player to the given channel.
local channel = Chat:CreateChannel("General", true, true)
server.PlayerConnected:Connect(function(player)
Chat:AddPlayerToChannel(channel, player)
end)
Parameters
channel
ChannelInfoplayer
PlayerReturns
void
Removes the given player from the given channel.
local isGameRunning = true
local function ResetGame(players)
for k, v in pairs(players) do
Chat:RemovePlayerFromChannel(generalChannel, v)
Chat:AddPlayerToChannel(ghostChannel, v)
end
end
Parameters
channel
ChannelInfoplayer
PlayerReturns
void
Sets the default channel for the player. This will be the default channel text messages are sent to.
local generalChannel = Chat:CreateChannel("General", true, true)
server.PlayerConnected:Connect(function(player)
Chat:SetDefaultChannel(player, generalChannel)
end)
Parameters
channelInfo
ChannelInfoReturns
boolean
Displays the given text message to the channel's chat log with a speech bubble over the given player's head.
local generalChannel = Chat:CreateChannel("General", true, true)
server.PlayerConnected:Connect(function(player)
Chat:AddPlayerToChannel(generalChannel, player)
end)
Chat.TextMessageReceivedHandler:Connect(function(channel, player, message)
Chat:DisplayTextMessage(channel, player, message)
end)
Parameters
Returns
void
Updated 3 months ago