PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Lua Chat System — Server API — ChatService

Lua Chat System — Server API — ChatService

Oct 31 2018, 12:12 AM PST 10 min

< Return to Lua Chat System

ChatService is a singleton object that handles the server-side behavior of the Lua Chat System, namely ChatChannels and ChatSpeakers.

All ModuleScripts within the ChatModules Folder should return a function; that function will be called with the ChatService singleton (this object).

Methods

AddChannel

  • Parameters: string channelName
  • Description: Creates a ChatChannel object with the given name and returns it.
  • Returns: ChatChannel

RemoveChannel

  • Parameters: string channelName
  • Description: Remove a channel with the given name
  • Returns: void

GetChannel

  • Parameters: string channelName
  • Description: Returns the channel with the given name, or nil if it does not exist.
  • Returns: ChatChannel

AddSpeaker

  • Parameters: string speakerName
  • Description: Create and add a speaker to the chat with the given name, then returns it.
  • Returns: ChatSpeaker

RemoveSpeaker

  • Parameters: string speakerName
  • Description: Removes the speaker from the chat with the given name.
  • Returns: void

GetSpeaker

  • Parameters: string speakerName
  • Description: Returns the speaker with the given name, or nil if it doesn’t exist.
  • Returns: ChatSpeaker

GetChannelList

  • Parameters: none
  • Description: Returns a list of the names of all non-private channels in the chat.
  • Returns: array<string>

GetAutoJoinChannelList

  • Parameters: none
  • Description: Returns a list of the names of all channels in the chat with AutoJoin set to true.
  • Returns: array<string>

RegisterFilterMessageFunction

  • Parameters: string functionId, function func
  • Description: Registers a filter function to the chat identified by functionId. Any changes to the message will persist and be displayed when the message makes it through all of the other filter functions. This function is passed the speaker’s name, the message object, and the channel the message originated in.
  • Returns: void
  • Example:
-- Paste this example into a ModuleScript within the ChatModules folder.
-- This example filters a keyword, and if successful, sets the chatColor of the message
local functionId = "greenText"
local keyword = "#green"
local chatColor = Color3.new(0, 1, 0) -- green

local function doFilter(speaker, messageObject, channelName)
	-- Check if the message contains the keyword
	local start, finish = string.find(messageObject.Message, keyword)
	if start and finish then
		-- Remove (filter) the keyword from the message, also setting the ChatColor
		messageObject.Message = string.gsub(messageObject.Message, keyword, "")
		messageObject.ExtraData.ChatColor = chatColor
	end
end

local function runChatModule(ChatService)
	ChatService:RegisterFilterMessageFunction(functionId, doFilter)
end
 
return runChatModule

UnregisterFilterMessageFunction

  • Parameters: string functionId
  • Description: Unregisters a filter function (registered by RegisterFilterMessageFunction) given its identifier, functionId.
  • Returns: void

RegisterProcessCommandsFunction

  • Parameters: string functionId, function func
  • Description: Registers a process command function to the chat identified by functionId. Before a message is filtered, it will pass through func (and other functions registered by this). The function func should check whether the message invokes a command. If so, perform the action of the command and return true. Returning true indicates the message was indeed a command and should not be displayed. The function can be unregistered using UnregisterProcessCommandsFunction.
  • Returns: void
  • Example:
-- Paste this example into a ModuleScript within the ChatModules folder.
local functionId = "getPizza"
local command = "/pizza"
local toolId = 22596452 -- Pepperoni pizza slice gear
local function processCommand(speakerName, message, channelName)
	if string.sub(message, 1, command:len()) == command then
		local model = game:GetService("InsertService"):LoadAsset(toolId)
		local tool = model:GetChildren()[1]
		local speaker = ChatService:GetSpeaker(speakerName)
		local player = speaker:GetPlayer()
		tool.Parent = player.Backpack
		return true
	end
	return false
end

local function runChatModule(ChatService)
	ChatService:RegisterProcessCommandsFunction(functionId, processCommand)
end
 
return runChatModule

UnregisterProcessCommandsFunction

  • Parameters: string functionId
  • Description: Unregisters a command processor (registered by RegisterProcessCommandsFunction) given the identifier, functionId.
  • Returns: void

Events

ChannelAdded

  • Parameters: string channelName
  • Description: Fires when a channel is added to the chat.

ChannelRemoved

  • Parameters: string channelName
  • Description: Fires when a channel is removed from the chat.

SpeakerAdded

  • Parameters: string speakerName
  • Description: Fires when a speaker is added to the chat.

SpeakerRemoved

  • Parameters: string speakerName
  • Description: Fires when a speaker is removed from the chat.