LUA:Freezing Ents
From GMod Wiki
Lua: Freezing Entities |
Description: | Freeze Weapon |
Original Author: | KillerLUA |
Created: | November 25, 2010 |
Contents |
Introduction
In this tutorial, I will be showing you how to add a weapon that freezes other props, it's quite basic but the main reason is to show you how to freeze entities with LUA as Player:Freeze().
Setting up the files
Addon
Create a new file called shared.lua inside YourAddon/lua/Weapons/swep_freeze
Gamemode
Create a new file called shared.lua inside YourGamemode/entites/weapons/swep_freeze
Coding it
The point of this tutorial is not to show you how to add a weapon, the code I will describe will be mostly the freeze code.
if SERVER then --Is this running on the server AddCSLuaFile("shared.lua") --Send this file to the client for usage end if CLIENT then --Is this running on the client 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? end SWEP.Author = "KillerLUA" --Author SWEP.Instructions = "Left/Right click to freeze" --How to use SWEP.Contact = "" --Contact SWEP.Purpose = "Freezes Props" --Purpose SWEP.ViewModelFOV = 62 --View Model Positioning SWEP.ViewModelFlip = false --Ignore this :D SWEP.AnimPrefix = "stunstick" --Preset animation info, ignore this too SWEP.Spawnable = false --Not spawnable by non-admin players SWEP.AdminSpawnable = 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 --Automatic weapon? SWEP.Primary.Ammo = "" SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = 0 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "" function SWEP:Initialize() --When the SWEP is first seen by the client/server if SERVER then self:SetWeaponHoldType("normal") end --Set the hold type end function SWEP:PrimaryAttack() --When you left click (by default) if CurTime() < self.NextStrike then return end if SERVER then self:SetWeaponHoldType("melee") timer.Simple(0.3, function(wep) if wep:IsValid() then wep:SetWeaponHoldType("normal") end end, self) --Checks that the swep hasn't quickly been switched end self.Owner:SetAnimation(PLAYER_ATTACK1) self.Weapon:EmitSound(self.Sound) ---Emit a sound, the stunstick wack self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER) self.NextStrike = CurTime() + .4
We need to be the server to do this so make sure we are!
if CLIENT then return end --No clients past here
If what we're looking at isn't valid then do nothing!
if not ValidEntity(self.Owner:GetEyeTrace().Entity) then return end
First, we get the owner of the weapon, then what they are looking at. We set the move info to none (Frozen).
self.Owner:GetEyeTrace().Entity:SetMoveType(MOVETYPE_NONE)
Back to the rest of the code
end function SWEP:SecondaryAttack() --When you right click (by default) self:PrimaryAttack() --Run Primary Attack end
In this script we create the stunstick animation and run payday on our victim.