Hey guys. So I’ve been planning on starting to learn Lua code for a long time and today I decided to sit my self down and learn. So far, I’ve created 3 pieces of code that fully work and here they are.
Now a heads up, these are the most simplest scripts ever so don’t expect big scripts haha.
hook.Add( "PlayerSay", "SteamIDCommand", function( ply, text, team ) -- Took me a while to understand how hooks worked because i'm dumb af local steamid64 = ply:SteamID64() local steamid = ply:SteamID() if( text == "!steamid") then -- Creation of the command !steamid if steamid64 == (nil) then -- Checking if steamid64 is available - if it's not then ply:SteamID64() will defaultly return as a "nil value" ply:ChatPrint("Your SteamID64 is currently unretrieveable, please try again later") -- Prints chat to the player that their SteamID64 is unavailable. else ply:ChatPrint("Your SteamID 64 is" .. steamid64) -- Concatenating string and variable to produce final product end if steamid == (nil) then ply:ChatPrint("Your SteamID is currently unretrieveable, please try again later") else ply:ChatPrint("Your SteamID is" .. steamid) end end)
^ This code allows for a player to do !steamid to retrieve both of their steamid’s.
Here it is working. Not sure how to make the ChatPrint to go underneath the command like a normal message would but meh whatever.
hook.Add( "PlayerSay", "RetrievePlayerNamesCommand", function ( ply, text, team) -- Since it's a command, it's going to have to use this hook if ( text == "!allplayers") then -- Creation of the command !allplayers for k, v in pairs(player.GetAll()) do -- Runs through the table which is returned by "player.GetAll()" ply:ChatPrint(v:Nick() .. " ") -- v = player assigned to each key of the table - used as a substitute for ply since we're inside the for loop. end -- Only just realised that you have to end for loops - silly me. end end)
^ This bit of code prints out the names of all the players online once you type in !allplayers
As you can see it works, but again the darn print is above the command. Maybe I just used the wrong function (ply:ChatPrint)
hook.Add( "PlayerSay", "KillPlayerCommand", function ( ply, text, team) -- Another command, another hook. commandcheck = string.sub(text, 1, 5) -- Counted the letters in "kill" + the "!", this will check the first 5 letters of the string. This may conflict with other scripts that include this word e.g !killall playername = string.sub(text, 7) -- Makeshift "arguement" for this command if ( commandcheck == "!kill" or "/kill" ) then -- Makes sure that the first 5 letters of the string are indeed "/kill" or "!kill" for k, v in pairs(player.GetAll()) do -- Runs through the player table if v:Nick() == playername then -- so that we can make sure the player is indeed online. ply:Kill() -- Kills the unfortunate player that is chosen else ply:ChatPrint("Player could not be found") -- If any of the values in the player table do not match up with "playername" then this means the player does not exist or is offline. end end end end)
^ When I made this I felt so proud of my self, even though it literally took me 3 minutes. I guess you pick up on Lua quite quickly.
There’s me dead by my own hands.
And that’s what happens when you try to kill your totally real friends.
I’d like for you guys to give me some suggestions on where to start next, anyways I could improve these scripts as I’ve seen people do these kind of scripts in like two lines lmao.
Also bare in mind that this was my first attempt at coding in Lua, the session was about 20 minutes so yeah.