Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can you make some sort of chat activated door that opens when you say a specific word?

I'm definitely not a pro at scripting and would like to know how. I have searched it up and it never really comes up with anything I want. I know it is possible because I have seen it on multiple games. Thanks for any help you can provide!

1 comment

0

thank you! sorry by the way...

3 answers

#Chat, how to use.

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 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.

#PARAMATERS;

game.Players.PlayerAdded:Connect(function(plr)
       plr.Chatted:Connect(function(msg)

       print(msg.Recipient, " Wrote;" msg)
end)

#CODE SAMPLES

Playing/Spectating Team

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)

#PLR.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)

From maxpax2009, a professional scripter.

2 comments

0

lol who downvoted this!?

0

it took me forever to write this.

You can use player.Chatted:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if msg == ":open" then
			workspace.Door.CanCollide = false --use the door here
			wait(1)
			workspace.Door.CanCollide = true
		end
	end)
end

Happy to help, b_mni.

This is really simple, although this is not a script request site. But I will give it to you anyways...

So, make a script in ServerScriptService. And then paste this in:

game.Players.PlayerAdded:Connect(function(plr) -- Checks if a player joined the game
	plr.Chatted:Connect(function(msg) -- Checks if the player chatted
		print("Player chatted "..msg.."!")
		if string.lower(msg) == "dog" then -- Whatever you want the user to say to trigger the event
			local part = game.Workspace.Part -- Gets the part
			part.Transparency = 0.5
			part.CanCollide = false
			wait(5)
			part.Transparency = 0
			part.CanCollide = true
		end
	end)
end)

I hope this helped!

~ Dan_PanMan, advanced scripter.

2 comments

0

He's obviously not going to learn anything from this, at least teach him about how to use; "Chatted", instead of being a slob. And i don't get why you're down-voting other peoples answers.

0

Time to teach you a lesson by down-voting your answer and up-voting the other guys answer, so yours doesn't get accepted. 

This discussion is preserved as it was. New answers, comments, and votes are closed.