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

Chatted

The Chatted event fires when a Player types a message and presses enter in Roblox’s provided chat bar. This is done using some Lua bindings by the default chat script. You can prevent players from chatting by using StarterGui/SetCoreGuiEnabled and disabling the Chat Enum/CoreGuiType.

Chat Commands

Using this event and some string manipulation functions like string.sub and string.lower, it is possible to create chat commands, even with arguments like player names. Usually, commands are prefixed such as /heal PlayerName. To check for a prefix in a string, use string.sub on the message to check a substring of the message: string.sub(message, 1, 6) == "/heal " (note the inclusion of the space) . Then, extract the rest of the command using string.sub again: string.sub(message, 7) will be equal to the player name. Check if that player exists, and if so, perform the command’s action (in this example, healing them). Check the code samples for examples of chat commands.

Filtering

The message text fired with this event is unfiltered. If you are displaying player input like chat to other players in any form, it must be filtered using Chat/FilterStringAsync. Keep this in mind when creating your own chat systems; if your game does not properly filter chat it may have moderation action taken against it.

Parameters

Name Type Default Description

The content of the message the player typed in chat

The player that the message was intended for (nil if the player was not whispering to another player)


Code Samples


Player.Chatted

=== Setting chatted for all players ===
There is an easy way to make the Chatted event registered on all players. Simply use the /Players/PlayerAdded event in combination with this event.

game.Players.PlayerAdded:Connect(function(player)
  player.Chatted:Connect(function(msg)
        -- do stuff with msg and player
  end)
end)

Join Team Command

This code sample allows any player to chat "/jointeam " where is the name of a team. It uses string manipulation using string.sub and string.lower to make the command case-insensitive and allow for partial matches. For example, “/jointeam red” will match the team “Red Robins”.

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

-- Command to choose a team (note the trailing space)
local joinCommand = "/jointeam "

local function findTeamByName(name)
	-- First, check for the exact name of a team
	if Teams:FindFirstChild(name) then return Teams[name] end
	-- Let's check for case-insensitive partial matches, like "red" for "Red Robins"
	for _, team in pairs(Teams:GetChildren()) do
		if team.Name:sub(1, name:len()):lower() == name:lower() then
			return team
		end
	end
	-- If we get to this point, no team matched the one we were looking for :(
end

local function onPlayerChatted(player, message, recipient)
	-- Note: string.sub(message, ...) is the same as message:sub(...)
	if message:sub(1, joinCommand:len()):lower() == joinCommand:lower() then
		-- Matched "/JOINTEAM xyz" to our join command prefix "/jointeam "
		local teamName = message:sub(joinCommand:len() + 1) -- Cut out the "xyz" from "/jointeam xyz"
		local team = findTeamByName(teamName)
		if team then
			-- Set the team!
			player.Team = team
			player.Neutral = false
		else
			-- Tell the player that team could not be found :(
			player.Team = nil
			player.Neutral = true
		end
	end
end

local function onPlayerAdded(player)
	player.Chatted:connect(function (...)
		onPlayerChatted(player, ...)
	end)
end

-- Listen for players being added
for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Playing/Spectating Teams

This code sample, although lengthy, is quite simple: detect when a player chats /play, then put them on the “Playing” team. When they die, move them back to the “Spectating” team.

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

-- Two teams: one for spectators (default) and one for those playing the game
local teamPlaying = Teams.Playing
local teamSpectators = Teams.Spectating
-- Command to play the game
local playCommand = "/play"

local function play(player)
	-- Set the player's team using one of the following:
	player.Team = teamPlaying
	player.TeamColor = teamPlaying.TeamColor
	-- Respawn the player (moves them to spawn location)
	player:LoadCharacter()
end

local function onPlayerDied(player, character)
	-- When someone dies, put them on the spectator team
	player.Team = teamSpectators
end

local function onPlayerSpawned(player, character)
	local human = character:WaitForChild("Humanoid")
	human.Died:connect(function () onPlayerDied(player, character) end)
end

local function onPlayerChatted(player, message)
	if message:sub(1, playCommand:len()):lower() == playCommand then
		play(player)
	end
end

local function onPlayerAdded(player)
	-- Listen for this player spawning
	if player.Character then
		onPlayerSpawned(player, player.Character)
	end
	player.CharacterAdded:connect(function ()
		onPlayerSpawned(player, player.Character)
	end)
end

-- Listen for players being added
for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
Players.PlayerAdded.Connect(onPlayerAdded)