Before you start, this tutorial assumes you know how events work and basic understanding on lua.
We'll take a look at the Chatted event, which is an event from the player object. You could use whatever way you want to reference the player, but im going to assume you're using local scripts.
The chatted event is fired when the player says something in the chat, this is done using some lua bindings by the default chat script. It has 2 Parameters:
message: it's type is string and is the content inside the player's message recipient: it is an instance, it's the player the message was intended for (nil if the player was not whispering to another player)
Well, we can use it for commands(what we'll do today) and whatever else you may need it for.
We are finally at the point everyone was waiting for, how to make a command. First, we'll need to reference the player and set up the chatted event.
Player = game.Players.LocalPlayer Player.Chatted:Connect(function(msg, recipient) --Our code will be inside here. end)
Now that we have it set up, we'll check first if the player is whispering someone, you may want to do something inside it but for now i'll just return nil, since i dont think we want commands to be ran when the player is whispering. This code will be inside the event.
if recipient then return nil end
Now, what this does is we check if the recipient exists, which if you remember from up above, would mean if the player is whispering someone, if it is, it would return true and execute the code there, now, i added a return since i dont want the rest of the code to run when the player is whispering, but you may want to do something there so its up to preference.
We'll finally actually check for if the player typed a command, first, we'll separate the command from the arguments(if there are any)
commandSplit = string.split(msg, ' ') command = commandSplit
```Lua What we do here is use string.split() to split the player's message by every space, then assiging commandSplit to the command variable, meaning we get the command. (The string.split() function will split our command in a table, in the same order that it was spaced, just to clarify) Now, we're going to check if the player actually typed a command, we just use a simple if statement. (note, replace "comandName" with the name of your command) ```Lua
if command == "comandName" then
--code for your command here end
Now that we're done checking all these stuff, we'll get to the fun part!
So, for this tutorial, i'll just make a simple command, for learning, but yours wont be exactly like mine. We'll make a part invisible when the player types the command, and if its already invisible, it will be visible again.
myPart = game.Workspace.Part --For more efficiency, declare myPart outside of the event. if myPart.Transparency == 0 then myPart.Transparency = 1 else myPart.Transparency = 0 end
What we do here is we first declare myPart, and then assign it to game.Workspace.Part. Then we check if the part's Transparency is 0, if it is, we change the Transparency to 1, else, it changes to 0.
Thats it, you should already have enough to make your own cool commands! If you got lost during the tutorial, here is the whole code we made here.
Player = game.Players.LocalPlayer myPart = game.Workspace.Part Player.Chatted:Connect(function(msg, recipient) commandSplit = string.split(msg, ' ') command = commandSplit if recipient then return nil end if command == "comandName" then if myPart.Transparency == 0 then myPart.Transparency = 1 else myPart.Transparency = 0 end end end)
Thanks for reading my tutorial and i hope you have a fun time making commands with your fresh knowledge!