Last active
November 4, 2021 17:01
-
-
Save slonkazoid/122fe79cef6a66956bd1768604458aa9 to your computer and use it in GitHub Desktop.
Command parsing with arguments in Particubes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- define your commands here | |
commands = { | |
help = { | |
description = "List all commands, or see one's description", | |
exec = function(cmd) | |
if cmd[2] ~= nil then -- if user supplied a command name | |
if commands[cmd[2]] ~= nil then -- and if that command exists | |
print(cmd[2] .. ": " .. commands[cmd[2]].description) -- print the command's description | |
else -- if it does not exist | |
print("Command not found: " .. cmd[2]) -- inform the user | |
end | |
else -- if the user wants to know all commands | |
print("Available commands: ") | |
for cmdname in pairs(commands) do -- in all commands | |
print(cmdname) -- print the name | |
end | |
end | |
end | |
}, | |
} | |
Client.OnChat = function(m) | |
-- replace with your command prefix, can be any length | |
local pfx = '%.' | |
if m:find("^" .. pfx) then | |
-- remove prefix | |
local full = m:gsub("^" .. pfx, "") | |
if (full == "") then return end -- if no command is supplied, end command parsing | |
-- make a table for command and arguments | |
local cmd = {} | |
-- split the command by spaces and add to table | |
for part in full:gmatch("([^ ]+)") do | |
table.insert(cmd, part) | |
end | |
if commands[cmd[1]] then -- if the command exists | |
commands[cmd[1]].exec(cmd) -- run it | |
else print("Command not found: " .. cmd[1]) end -- if it doesn't, inform the user | |
else -- default behavior | |
print(Player.Username .. " said " .. m) | |
Player:TextBubble(m, 3, true) | |
end | |
end |
My first attempt at lua but thought this was neat 😄
1 fullsend = {
2 description = "Teleport to Crosshair",
3 exec = function(cmd)
4 local impact = Player:CastRay()
5 if impact.Block ~= nil then
6 print("RayCast:" .. " X: " .. impact.Block.Pos.X .. " Y: " .. impact.Block.Pos.Y .. " Z: " .. impact.Block.Pos.Z .. "!")
7 Player.Position = Number3(impact.Block.Pos.X, impact.Block.Pos.Y+15, impact.Block.Pos.Z)
8 end
9 end
10 }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can add your own commands. For example, I added a pos command that prints your position