Using the Chatted Event: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>LocalChum
Enabled syntax highlighting for the code snippets
>NXTBoy
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
== Setting Chatted for All Players ==
== Setting Chatted for All Players ==
There is an easy way to make the Chatted event registered on all players. Simply use the [[PlayerAdded]] event in combination with the Chatted event.
There is an easy way to make the Chatted event registered on all players. Simply use the [[PlayerAdded]] event in combination with the Chatted event.
<code lua>
{{lua|=
Game.Players.PlayerAdded:connect(function(player)
Game.Players.PlayerAdded:connect(function(player)
  player.Chatted:connect(function(msg)
    player.Chatted:connect(function(msg)
 
        -- do stuff with msg and player
  end)
    end)
end)
end)
</code>
}}


That is the basic structure of a chatted event connecting to any player that joins a game. In that script, you can use the variables 'msg' for the message chatted, and 'player' for whoever chatted the message.
That is the basic structure of a chatted event connecting to any player that joins a game. In that script, you can use the variables 'msg' for the message chatted, and 'player' for whoever chatted the message.
Line 15: Line 15:
== Setting Chatted for One Player ==
== Setting Chatted for One Player ==
A simple way to make the Chatted event only register for one user is to detect if the incoming player's name is a certain string. This is shown in the following example.
A simple way to make the Chatted event only register for one user is to detect if the incoming player's name is a certain string. This is shown in the following example.
<code lua>
{{lua|=
Game.Players.PlayerAdded:connect(function(player)
game.Players.PlayerAdded:connect(function(player)
  if player.Name == "MrNicNac" then
    if player.Name == "MrNicNac" then
    player.Chatted:connect(function(msg)
        player.Chatted:connect(function(msg)


    end)
       end)
  end
    end
end)
end)
</code>
}}


You can change "MrNicNac", to the name you want the chatted event to register for.
You can change "MrNicNac", to the name you want the chatted event to register for.
Line 29: Line 29:
== Setting Chatted for a Group of Players ==
== Setting Chatted for a Group of Players ==
This is a bit more complicated. Here we are going to use a table that holds string indexes of the players' names. You want to have the Chatted event register to when they enter the game. We will use the indexes, and not the values, so we can avoid making a longer-than-needed check system.
This is a bit more complicated. Here we are going to use a table that holds string indexes of the players' names. You want to have the Chatted event register to when they enter the game. We will use the indexes, and not the values, so we can avoid making a longer-than-needed check system.
<code lua>
{{lua|=
local People = {
local group = {
     ["MrNicNac"] = true;
     ["MrNicNac"] = true;
     ["Yami"] = true;
     ["Yami"] = true;
     ["Joey Wheeler"] = true;
     ["Joey Wheeler"] = true;
     ["mattchewy"] = true;
     ["mattchewy"] = true;
    ["Tomtomn00"] = true;
}
}


Game.Players.PlayerAdded:connect(function(player)
game.Players.PlayerAdded:connect(function(player)
  if People[player.Name] then  
    if group[player.Name] then  
    player.Chatted:connect(function(msg)
        player.Chatted:connect(function(msg)
      --chatted code  
            --chatted code  
    end)
        end)
  end
    end
end)
end)
</code>
}}


That will only make the chatted event register to the people who join with the same name as those in the 'People' table.
That will only make the chatted event register to the people who join with the same name as those in the 'People' table.

Latest revision as of 20:14, 9 May 2012

The chatted event fires whenever a player chats using the Roblox Chat feature in a game. This brief tutorial will show you how to setup a chatted event and make something happen.

Setting Chatted for All Players

There is an easy way to make the Chatted event registered on all players. Simply use the PlayerAdded event in combination with the Chatted event.

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

That is the basic structure of a chatted event connecting to any player that joins a game. In that script, you can use the variables 'msg' for the message chatted, and 'player' for whoever chatted the message.

Setting Chatted for One Player

A simple way to make the Chatted event only register for one user is to detect if the incoming player's name is a certain string. This is shown in the following example.

game.Players.PlayerAdded:connect(function(player)
    if player.Name == "MrNicNac" then
        player.Chatted:connect(function(msg)

        end)
    end
end)

You can change "MrNicNac", to the name you want the chatted event to register for.

Setting Chatted for a Group of Players

This is a bit more complicated. Here we are going to use a table that holds string indexes of the players' names. You want to have the Chatted event register to when they enter the game. We will use the indexes, and not the values, so we can avoid making a longer-than-needed check system.

local group = {
    ["MrNicNac"] = true;
    ["Yami"] = true;
    ["Joey Wheeler"] = true;
    ["mattchewy"] = true;
    ["Tomtomn00"] = true;
}

game.Players.PlayerAdded:connect(function(player)
    if group[player.Name] then 
        player.Chatted:connect(function(msg)
             --chatted code 
        end)
    end
end)

That will only make the chatted event register to the people who join with the same name as those in the 'People' table.