LUA:DarkRP Chat Command

From Official DarkRP Wiki
Jump to: navigation, search
Lua: DarkRP Chat Command
Page white text.png Description:How to create a new chat command for DarkRP 2.5.0.
File:User.png Original Author:KillerLUA
16px Created:April 25, 2010
File:Table edit.png Updated:August 19, 2013
This page is available in the following languages:
Данная версия страницы доступна на следующих языках:
Jarvis-flag 8010.png English | France-flag.png Français

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 " .. DarkRP.formatMoney(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 <Amount>

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 " .. DarkRP.formatMoney(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.