LUA:Pay Day Baton

Lua: Pay Day Baton
Page white text.png Description:How to create a pay day baton.
User.png Original Author:KillerLUA
Calendar.png Created:March 13, 2010
Table edit.png Updated:December 26, 2014
This page is available in the following languages:
Данная версия страницы доступна на следующих языках:
Jarvis-flag 8010.png English | France-flag.png Français


Welcome to the tutorial, in this tutorial. I will not only be showing you how to make a swep, I will be showing you one that uses custom meta functions!

Setting up the filesEdit

Create a new file called shared.lua inside the DarkRP modification addon in lua/weapons/pay_day_baton (see here).

Coding itEdit

Add the following code into shared.lua:

if CLIENT then --Client stuff
	SWEP.PrintName = "Pay Day Baton" --Name
	SWEP.Slot = 1
	SWEP.SlotPos = 3
	SWEP.DrawAmmo = false --Draw ammo in bottom right corner
	SWEP.DrawCrosshair = false --Draw crosshair?
SWEP.Author = "KillerLUA" --Author
SWEP.Instructions = "Left or right click to give the player a pay day" --How to use
SWEP.Contact = "" --Contact
SWEP.Purpose = "Gives people paydays" --Purpose
SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.AnimPrefix = "stunstick"
SWEP.Spawnable = true
SWEP.AdminOnly = true --Only spawnable by admins!
SWEP.NextStrike = 0
SWEP.ViewModel = Model("models/weapons/v_stunstick.mdl") --The model while being held
SWEP.WorldModel = Model("models/weapons/w_stunbaton.mdl") --World model (on the ground)
SWEP.Sound = Sound("weapons/stunstick/stunstick_swing1.wav") --The sound
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false 
SWEP.Primary.Ammo = ""
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""
function SWEP:Initialize()
function SWEP:PrimaryAttack()
	if CurTime() < self.NextStrike then return end
	timer.Simple(0.3, function() if self:IsValid() then self:SetHoldType("normal") end end) --Checks that the swep hasn't quickly been switched

	self.Weapon:EmitSound(self.Sound) ---Emit a sound, the stunstick wack
	self.NextStrike = CurTime() + 0.51
	if CLIENT then return end --No clients past here
	local trace = util.QuickTrace(self.Owner:EyePos(), self.Owner:GetAimVector() * 90, {self.Owner})

	local ent = self.Owner:getEyeSightHitEntity(nil, nil, function(p) return p ~= self.Owner and p:IsPlayer() and p:Alive() end)

        if not IsValid(ent) or (self.Owner:EyePos():Distance(ent:GetPos()) > 90) or (not ent:IsPlayer() and not ent:IsNPC()) then

	ent:payDay() --Give them a payday
function SWEP:SecondaryAttack()
	self:PrimaryAttack() --Run Primary Attack

In this script we create the stunstick animation and run payday on our victim.