LUA:DarkRP Chat Command

From GMod Wiki

Jump to: navigation, search
Lua: DarkRP Chat Command
Page white text.png Description:How to create a new chat command for DarkRP 2.4.1
link=User:KillerLUA Original Author:KillerLUA
Calendar.png Created:April 25, 2010

Contents


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

In this tutorial we will be using the default DarkRP main.lua file!

You could also paste this in a new lua file in gamemodes/modules. It should work just the same, but the work wont get lost at updates.

Coding it

Add the following code to the bottom of main.lua:

 
local function CreateCash(ply, args)
	if args == "" then return "" end
 
	if not tonumber(args) then
		return ""
	end
	local amount = math.floor(tonumber(args))
 
	if amount <= 1 then
		Notify(ply, 1, 4, string.format(LANGUAGE.invalid_x, "argument", ""))
		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)
	DarkRPCreateMoneyBag(tr.HitPos, amount)
 
	return ""
end
AddChatCommand("/createcash", CreateCash)
AddChatCommand("/createmoney", CreateCash)
AddChatCommand("/cashcreate", CreateCash)
AddChatCommand("/moneycreate", CreateCash)
 

Let's take the code apart

First, we check if the player has provided anything, E.G /dropmoney <Amount>

 
if 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
		Notify(ply, 1, 4, string.format(LANGUAGE.invalid_x, "argument", ""))
		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:

 
DarkRPCreateMoneyBag(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 ""
 

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

Yarin Kaul Icon ArrowSquare32.png Back to DarkRP Tutorials


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox