(Beginner at LUA) How do I make a command.

Hello there. Before you ask, yes, I have searched how to make a command, I’ve tried plenty of different stuff, all of which didn’t really work.

Now, before you ask this as-well, I JUST began lua coding (Literally, started a week ago and stopped after a day, began again today), so please, do not be all up on my ass about how much I fail.

This is the most recent way I tried to create a command, so please take a look at it:



include( 'mysql.lua' )
require( "chatcommand" )
 
chatcommand.Add( "/givecash", GivePlayerCash )
chatcommand.Add( "/setcash", SetPlayerCash )
chatcommand.Add( "/setadmin", SetPlayerAdmin )


I have that in a seperate .lua file that I run under init.lua. Now, this is the module I use to create the command (not mine, took it and didn’t edit anything):



if not SERVER then return end

local table = table
local string = string
local hook = hook

module "chatcommand"

-- Settings
Blocking		= false
PrefixPrivate	= "/"
PrefixPublic	= "!"

local _commands = {}

function Add( name, func )
	_commands[name:lower()] = func
end

function Remove( name )
	_commands[name:lower()] = nil
end

function Run( player, command, arguments )
	local func = _commands[command:lower()]
	
	if not func then
		if player:IsValid() and Blocking == true then
			player:ChatPrint( "Unknown command: '" .. command .. "'
" )
		end
		return false
	end
	
	func( player, command, arguments )
	
	return true
end

local function ParseChat( player, text, team )
	local txt = text
	local prefix = txt:sub( 1,1 )
	if prefix ~= PrefixPublic and prefix ~= PrefixPrivate then return end
	
	txt = txt:sub( 2 )

	local cmd = txt:match( "^(%S+)" )
	txt = txt:gsub( "^(%S+)", "", 1 )
	if cmd == txt then return "" end
	
	-- This chunk of code is from Lexi, thanks :3
	-- http://www.facepunch.com/showthread.php?t=827179
	local quote = txt:sub( 1, 1 ) ~= '"'
	local ret = {}
	for chunk in txt:gmatch( '[^"]+' ) do
		quote = not quote
		if quote then
			table.insert( ret, chunk )
		else
			for chunk in chunk:gmatch( "%S+" ) do
				table.insert( ret, chunk )
			end
		end
	end
	
	local exists = Run( player, cmd, ret )
	
	return (exists or Blocking) and ((prefix == PrefixPublic) and text or "") or text
end
hook.Add( "PlayerSay", "ChatCommand.ParseChat", ParseChat )


Can someone please show me how to make a command, because these things don’t work?

Also, these are the functions I use with the commands:



function GivePlayerCash( ply, oply, cash )

	admin = sql.QueryValue("SELECT admin FROM pinfo WHERE steamid = '"..SteamID.."'")
	ply:SetNWInt("admin", admin)
	if admin >= 1 then
		Money = ply:GetNWString("money")
		oply:SetNWInt("money", Money + cash)
		ply:ChatPrint("You gave "..oply:Nick().." "..cash.." dollars.")
	else
		ply:ChatPrint("You are not an admin!")
	end
	
end

function SetPlayerCash( ply, oply, cash )

	admin = sql.QueryValue("SELECT admin FROM pinfo WHERE steamid = '"..SteamID.."'")
	if admin >= 1 then
		oply:SetNWInt("money", cash)
		ply:ChatPrint("You set "..oply:Nick().."'s cash to "..cash.." dollars.")
	else
		ply:ChatPrint("You are not an admin!")
	end

end

function SetPlayerAdmin( ply, oply, adminlevel )

	admin = sql.QueryValue("SELECT admin FROM pinfo WHERE steamid = '"..SteamID.."'")
	if admin = 5 then
		if adminlevel >= 6 or adminlevel <= -1 then
			ply:ChatPrint("Invalid admin level (0-5)")
		else
			oply:SetNWInt("admin", adminlevel)
			ply:ChatPrint("You set "..oply:Nick().."'s admin level to "..adminlevel.."")
		end
	else
		ply:ChatPrint("You are not an admin!")
	end
end


I suppose I do not need to provide my database creation and shit, seeing as I actually took that from a tutorial and edited it (looks perfect, I have experience with mysql in the scripting language of PAWN for SA-MP.

Thanks, I am really looking forward to an answer.

Minimize use of global variables: http://www.lua.org/pil/4.2.html

This is one way to do it:



-- lua/autorun/server/nice_name.lua
if !SERVER then return end
local function GivePlayerCash(ply, oply, cash)
	-- Everything ok?
	if !ValidEntity(ply) or !ValidEntity(oply) or !cash then return end
	-- Get Steam ID of ply.
	local SteamID = ply:SteamID()
	-- Get group.
	local admin = sql.QueryValue("SELECT admin FROM pinfo WHERE steamid = '"..SteamID.."'")
	-- Set group.
	ply:SetNWInt("admin", admin)
	-- is ply an admin?
	if admin >= 1 then
		Money = ply:GetNWString("money")
		oply:SetNWInt("money", Money + cash)
		ply:ChatPrint("You gave "..oply:Nick().." "..cash.." dollars.")
	else
		ply:ChatPrint("You are not an admin!")
	end
end

local function SetPlayerCash(ply, oply, cash)
	if !ValidEntity(ply) or !ValidEntity(oply) or !cash then return end
	local SteamID = ply:SteamID()
	local admin = sql.QueryValue("SELECT admin FROM pinfo WHERE steamid = '"..SteamID.."'")
	if admin >= 1 then
		oply:SetNWInt("money", cash)
		ply:ChatPrint("You set "..oply:Nick().."'s cash to "..cash.." dollars.")
	else
		ply:ChatPrint("You are not an admin!")
	end
end

local function SetPlayerAdmin(ply, oply, adminlevel)
	if !ValidEntity(ply) or !ValidEntity(oply) or !adminlevel then return end
	local SteamID = ply:SteamID()
	local admin = sql.QueryValue("SELECT admin FROM pinfo WHERE steamid = '"..SteamID.."'")
	if admin == 5 then
		if adminlevel >= 6 or adminlevel <= -1 then
			ply:ChatPrint("Invalid admin level (0-5)")
		else
			oply:SetNWInt("admin", adminlevel)
			ply:ChatPrint("You set "..oply:Nick().."'s admin level to "..adminlevel.."")
		end
	else
		ply:ChatPrint("You are not an admin!")
	end
end

-- We want to know what they say. 
hook.Add("PlayerSay","GetCommand", function(pl,Text)
	-- Get the length of the text.
	local Length = string.len(Text)
	--We can do this because the commands are around the same length.
	if Length > 11 then
		-- Lower the case.
		local Low = string.lower(Text)
		-- Get the beginning of the text.
		local Beginning = string.sub(Low,1,9)
		-- Do we find a command?
		if string.find(Beginning,"/givecash") then
			-- Get the rest of the text.
			local Rest = string.sub(Low,11,Length)
			-- Get the arguments.
			local Args = string.Explode(" ", Rest)
			-- Are all the arguments present and is the second argument a number?
			if !Args[1] or !Args[2] or !tonumber(Args[2]) then
				pl:ChatPrint("Missing arguments.")
				return
			end
			local Found
			local oply
			-- Get all the players and loop through them.
			for k, v in pairs(player.GetAll()) do
				-- Does the nick name of the player contain the first argument and is it not the command giver? -> You can remove v != pl if you want to be able to apply this command to yourself.
				if string.find(string.lower(v:Nick()),Args[1]) and v != pl then
					-- Did we found another player?
					if Found then
						pl:ChatPrint("Two 2 or more players found.")
						return
					end
					-- set oply to the identified player.
					oply = v
					-- Yep, we found one.
					Found = true
				end
			end
			-- Did we found one? if yes then run our function, if no then inform the command giver.
			if Found then GivePlayerCash(pl, oply, tonumber(Args[2])) else pl:ChatPrint("No player found.") end
		elseif string.find(Beginning,"/setcash") then
			local Rest = string.sub(Low,10,Length)
			local Args = string.Explode(" ", Rest)
			if !Args[1] or !Args[2] or !tonumber(Args[2]) then
				pl:ChatPrint("Missing arguments.")
				return
			end
			local Found
			local oply
			for k, v in pairs(player.GetAll()) do
				if string.find(string.lower(v:Nick()),Args[1]) and v != pl then
					if Found then
						pl:ChatPrint("Two 2 or more players found.")
						return
					end
					oply = v
					Found = true
				end
			end
			if Found then SetPlayerCash(pl, oply, tonumber(Args[2])) else pl:ChatPrint("No player found.") end
		elseif string.find(Beginning,"/setadmin") then
			local Rest = string.sub(Low,11,Length)
			local Args = string.Explode(" ", Rest)
			if !Args[1] or !Args[2] or !tonumber(Args[2]) then
				pl:ChatPrint("Missing arguments.")
				return
			end
			local Found
			local oply
			for k, v in pairs(player.GetAll()) do
				if string.find(string.lower(v:Nick()),Args[1]) and v != pl then
					if Found then
						pl:ChatPrint("Two 2 or more players found.")
						return
					end
					oply = v
					Found = true
				end
			end
			if Found then SetPlayerAdmin(pl, oply, tonumber(Args[2])) else pl:ChatPrint("No player found.") end
		end
	end
end)


To give the command say: command, part of player nick, number

-Sorry wrong tab-