• How to Make a Simple Chat Command?
    12 replies, posted
Hello, I don't expect anyone to make the code for me, of course. But I was wondering if you could point me in the right direction I am trying to make a fun command for my Gmod Prop Hunt server. !cake "Player" A" gave "Player B" a yummy piece of chocolate cake. If you could tell me where I could learn how to do this, I'd really appreciate it. I'm new to Lua lol P.S. extra points if you know how to make it so that the command "auto predicts" what the person is typing. For example, instead of typing "!cake PlayerA" one could simply type "!cake Pla" Thanks a lot for all of your help, everyone on Facepunch
This might work hook.Add( "PlayerSay", "Cake", function( ply, text, team ) if string.sub( text, 1, 5 ) == "!cake" then local name = string.sub( text, 6 ) local found for k, v in ipairs( player.GetAll() ) do if string.sub( v:Nick(), 1, #name ) == name then found = v end end if IsValid( found ) then return ply:Nick() .. " gave " .. found:Nick() .. " a yummy piece of chocolate cake." end end end )
O.O Youre incredible Not only was that more than I asked for, but dang you were fast Thanks a lot for your help, I'll try to go test it now (keyword "try" because I only have myself to try it on at the moment and Idk if the command will respond to me giving it to myself lol)
but that's exactly what you asked for? Also, yeah it works on yourself, i tried it on myself. I could try disabling that if you want
No, no it's fine that it works on myself even better lol And I was only asking for someone to point me in the direction (like a tutorial or something). I hate making other people do the work for me. Not that I don't appreciate it! I just feel bad making you type all of that out
The only way I know of to display colored text in the chat is to use chat.AddText. However, this is clientside. This gives you two options. The first is to use the clientside chat hook GM:OnPlayerChat: hook.Add( "OnPlayerChat", "CakeCommand", function( ply, text, team ) -- Change CakeCommand to whatever if string.sub( text, 1, 5 ) == "!cake" then -- If the first 5 letters said are !cake then local name = string.sub( text, 7 ) -- The name of the player will be the last 7 letters local found -- This variable is set to nil and will be set to the player when found for _, v in ipairs( player.GetAll() ) do -- For every player on the server including bots if string.sub( string.lower( v:Nick() ), 1, #name ) == string.lower( name ) then -- If the first few letters are equal to the first few letters provided before then found = v -- The player has been found end end if IsValid( found ) then -- If there was a found player and they were valid chat.AddText( Color( 135, 206, 235 ), ply:Nick() .. " gave " .. found:Nick() .. " a yummy piece of chocolate cake." ) return true -- Surpress the message from displaying in the chat end end end ) This example isn't particularly good because it makes every player on the server check through every player, even if they didn't type the message. The second option is to use the serverside chat hook GM:PlayerSay: hook.Add( "PlayerSay", "CakeCommand", function( ply, text, team ) -- Change CakeCommand to whatever if string.sub( text, 1, 5 ) == "!cake" then -- If the first 5 letters said are !cake then local name = string.sub( text, 7 ) -- The name of the player will be the last 7 letters local found -- This variable is set to nil and will be set to the player when found for _, v in ipairs( player.GetAll() ) do -- For every player on the server including bots if string.sub( string.lower( v:Nick() ), 1, #name ) == string.lower( name ) then -- If the first few letters are equal to the first few letters provided before then found = v -- The player has been found end end if IsValid( found ) then -- If there was a found player and they were valid BroadcastLua( "chat.AddText( Color( 135, 206, 235 ), '" .. ply:Nick() .. " gave " .. found:Nick() .. " a yummy piece of chocolate cake.' )") return "" -- Surpress the message from displaying in the chat end end end ) This example isn't particularly good either because I used BroadcastLua because I'm lazy instead of the net library and net.Broadcast.
Oh, so I couldn't simply add: chat.AddText( Color( 135, 206, 235 ) (That was a suggestion on another forum)
This code is better because I stopped being lazy and actually did it properly. Just remember the serverside bit MUST be run serverside and the clientside bit MUST be run clientside: -- SERVERSIDE: util.AddNetworkString( "CakeCommand" ) hook.Add( "PlayerSay", "CakeCommand", function( ply, text, team ) -- Change CakeCommand to whatever if string.sub( text, 1, 5 ) == "!cake" then -- If the first 5 letters said are !cake then local name = string.sub( text, 7 ) -- The name of the player will be the last 7 letters local found -- This variable is set to nil and will be set to the player when found for _, v in ipairs( player.GetAll() ) do -- For every player on the server including bots if string.sub( string.lower( v:Nick() ), 1, #name ) == string.lower( name ) then -- If the first few letters are equal to the first few letters provided before then found = v -- The player has been found end end if IsValid( found ) then -- If there was a found player and they were valid net.Start( "CakeCommand" ) -- Begin the net thingy net.WriteEntity( ply ) -- Send the player who gave the cake net.WriteEntity( found ) -- Send the player receiving the cake net.Broadcast() -- Make sure all players see the message return "" -- Surpress the message from displaying in the chat end end end ) -- CLIENTSIDE: net.Receive( "CakeCommand", function() local ply = net.ReadEntity() -- Read the player who gave the cake local found = net.ReadEntity() -- Read the player who got the cake chat.AddText( Color( 135, 206, 235 ), ply:Nick() .. " gave " .. found:Nick() .. " a yummy piece of chocolate cake." ) end )
Ah, okay, I see. Thanks for all of your help, sir/madam
sorry, I tried to elaborate my post a bit more because I never explain anything properly
Ohh, okay So I usually put ULX commands here: garrysmod\addons\ulx\lua\ulx\modules\sh\ does that mean I would put the Serverside file in: garrysmod\addons\ulx\lua\ulx\modules\sh\ and the Clientside file in: \garrysmod\addons\ulx\lua\ulx\modules\cl\
This isn't a ULX thing though. Unfortunately I know nothing about ULX so I just assumed this was for a generic chat command. If you want it to be ULX then I can't really help with that because I have no clue how it works. Since I thought it was just generic, I just put it in these paths: \garrysmod\addons\cake\lua\autorun\client\ \garrysmod\addons\cake\lua\autorun\server\
Oh, oddly enough I put it in the ulx folder and it worked O.O Okay, I'll switch it over to that folder then. I really can't say how much I appreciate all that you've done. You've been a tremendous help ^-^
Sorry, you need to Log In to post a reply to this thread.