LUA:DarkRP Chat Command

Introduction
Welcome to the tutorial, in this tutorial. I will be showing you how to add a new chat command that creates money from nothing!

Setting up the files
The following code should be placed in a serverside file in a module. See Creating modules for more.

Coding it
Add the following code inside your serverside file:

local function CreateCash(ply, args) if args == "" or not tonumber(args) then return "" end local amount = math.floor(tonumber(args))

if amount <= 1 then DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("invalid_x", DarkRP.getPhrase("arguments"), "")) return "" end if ply:IsSuperAdmin then ply:ChatPrint("You created " .. tostring(amount) .. "$!") else ply:ChatPrint("You are not authorized to use this chat command!") return "" end

local trace = {} trace.start = ply:EyePos trace.endpos = trace.start + ply:GetAimVector * 85 trace.filter = ply

local tr = util.TraceLine(trace) DarkRP.createMoneyBag(tr.HitPos, amount)

return "" end DarkRP.defineChatCommand("createcash", CreateCash) DarkRP.defineChatCommand("createmoney", CreateCash)

And this in a shared file:

DarkRP.declareChatCommand{ command = "createcash", description = "Create cash!", delay = 1.5 } DarkRP.declareChatCommand{ command = "createmoney", description = "Create cash!", delay = 1.5 }

Let's take the code apart

First, we check if the player has provided anything and if it is a numeric value, e.g. /dropmoney 

if args == "" or not tonumber(args) then return "" end

Next, we check if the value they are trying to create is lower than 1, or if the player that is trying to create cash, is not a superadmin.

local amount = math.floor(tonumber(args))

if amount <= 1 then DarkRP.notify(ply, 1, 4, DarkRP.getPhrase("invalid_x", DarkRP.getPhrase("arguments"), "")) return "" end if ply:IsSuperAdmin then ply:ChatPrint("You created " .. tostring(amount) .. "$!") else ply:ChatPrint("You are not authorized to use this chat command!") return "" end

Next, we create a trace object of where the playing is looking at...

local trace = {} trace.start = ply:EyePos trace.endpos = trace.start + ply:GetAimVector * 85 trace.filter = ply

local tr = util.TraceLine(trace)

And finally, we create the money using the trace information:

DarkRP.createMoneyBag(tr.HitPos, amount)

When using DarkRP chat commands, you must return a empty string. Otherwise, all players will see the chatcommand and arguments in their chat boxes!

return ""

We then define the chat command, so it actually works:

DarkRP.defineChatCommand("createcash", CreateCash) DarkRP.defineChatCommand("createmoney", CreateCash)

In the shared file we then declare the command. This adds a delay to prevent spam and a short description that's used in the F1 help menu.

DarkRP.declareChatCommand{ command = "createcash", description = "Create cash!", delay = 1.5 } DarkRP.declareChatCommand{ command = "createmoney", description = "Create cash!", delay = 1.5 }

In this script, we add a chat command that creates cash from nothing.