Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What are the events that are used when a person chats and clicks something?

Asked by 8 years ago

I also want some examples for these and how to use them and when to use them.

3 answers

Log in to vote
0
Answered by 8 years ago

The event for when a player chats is called Chatted. Useful in admin commands, advanced chat filtering, etc.

Example:

game.Players.PlayerAdded:connect(function(p)
    p.Chatted:connect(function(msg,recipient)
        -- msg is what they wrote, recipient is the player they are whispering to, nil if they're not whispering
        print(p.Name.. " just said: " ..msg)
    end)
end)

When a player clicks, the event is called Button1Down. Useful when making buttons, tools, etc.

Example (LocalScript):

local p = game.Players.LocalPlayer
local mouse = p:GetMouse()

mouse.Button1Down:connect(function()
    print(p.Name.. " has clicked!")
end)

There are some other ways of detecting clicks, like ClickDetectors and Guis.

Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

When a person chats

The event that correlates to the client's chat is the Chatted event. This event is a member of the player instance and returns 2 arguments;

  • 1) What the person chatted

  • 2) The person that chatted it

So, an example for using this would be to determine whenever your player says a key phrase.

--First, define the player
local plr = game.Players.LocalPlayer
--Next the key phrase
local phrase = "bob"

--Now the chatted event
plr.Chatted:connect(function(msg)
    --Now you can use the 'match' function
    if msg:match(phrase) then
        print("Match")
    end
end)

Note: This should be in a localscript!



When a player clicks something

There are three ways for the client to click something. This can either be a GuiObject,ClickDetector, or a Tool.

ClickDetectors can be placed in parts to make it so you can detect clicking on the object. The event that correlates to ClickDetectors is the MouseClick event, which returns the player that clicked.

An example of using it would be printing out the name of the player that clicked.

workspace.Part.ClickDetector.MouseClick:connect(function(plr)
    print(plr.Name)
end

Note: ClickDetectors do not work with FilteringEnabled.


GuiObjects are things like a TextButton, or ImageButton. The evenst that correlate to GuiObjects are the MouseButton1Down, MouseButton1Up,MouseButton2Down,MouseButton2Up,MouseButton1Click, and MouseButton2Clickevents. All of the events do what they sound like they do. 1 is meaning the left mouse button and 2 is meaning the right. You can use any of these events according to your purposes or personal preference.

An example of using it would be to count how many times a gui was clicked.

--Predefine a value that will be incremented later
local num = 0

script.Parent.MouseButton1Down:connect(function()
    --Increment the value
    num = num + 1
    --Print it
    print(num)
end)

Note: Any scripts reguarding GuiObjects should be LocalScripts.


Tools are either HopperBins or Tools. The events that correlate to them are the same as for GuiObjects, but without 'mouse' in them. These events are members of the mouse object.

An example of using it would be to count how many times a tool was clicked, when equipped.

local num = 0

script.Parent.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        num = num + 1;
        print(num)
    end
    script.Parent.Unequipped:connect(function()
        num = 0
    end)
end)
Log in to vote
-1
Answered by 8 years ago

Here's the event to use for chatting: http://wiki.roblox.com/index.php?title=API:Class/Player/Chatted

And here's the object to use for clicking on parts, depending if you want to use parts or GUI's. GUI's are different, but for parts it would be this: http://wiki.roblox.com/index.php?title=API:Class/ClickDetector

I hope this helped, and please comment if there is anymore that you would like to know. These wiki articles should help.

Answer this question