[HELP]Trouble In Terrorist Town: What's Wrong with my SWEP?

[lua]if SERVER then
– DISABLED
AddCSLuaFile( “shared.lua” )
end

SWEP.HoldType = “normal”

if CLIENT then
SWEP.PrintName = “HeartBeatBomb”
SWEP.Slot = 8
SWEP.SlotPos = 0

SWEP.ViewModelFOV = 55

SWEP.EquipMenuData = {
type=“Weapon”,
model=“models/props_lab/reciever01b.mdl”,
desc=“A Heart Beat Bomb… Can regenerate health when extremely low, and blows up on Death. You die after 3 minutes.”
};

SWEP.Icon = “VGUI/ttt/icon_cse”
end

SWEP.Base = “weapon_tttbase”

SWEP.Spawnable = true
SWEP.AdminSpawnable = false
SWEP.ViewModel = “models/weapons/v_crowbar.mdl”
SWEP.WorldModel = “models/weapons/v_crowbar.mdl”
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.DrawCrosshair = false
SWEP.ViewModelFlip = false
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = “none”
SWEP.Primary.Delay = 1.0

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = “none”
SWEP.Secondary.Delay = 1.0

SWEP.Kind = WEAPON_EQUIP
– DISABLED
SWEP.CanBuy = {ROLE_TRAITOR} – T’s are the only ones who can purchase this.
SWEP.LimitedStock = true – Limits to only be bought once.
SWEP.WeaponID = NONE

SWEP.AllowDrop = false

SWEP.NoSights = true

self.Owner = Player

function SWEP:OnDrop()
self:Remove()
end

function SWEP:Initialize()
timer.Create(“timer”, 240, 1, Explode, ply1, ent, killer )
timer.Create(“HealthRegen”, 1, 0, HealthRegen)
HealthRegen()
self.Owner:ChatPrint(“HeartBeatBomb Planted on Yourself!”)
if self.Owner:Health < 10 then
function Explode( ply1, ent, killer )
end
end

function Explode( ply1, ent, killer )
if ply1 ~= killer then
local explode = ents.Create( “env_explosion” )
explode:SetPos( Player:pos() )
explode:SetOwner ( Player() )
explode:Spawn()
explode:SetKeyValue( “iMagnitude”, “270” )
explode:Fire( “Explode”, 0, 0 )
explode:EmitSound( “weapon_AWP.Single”, 500, 500 )
end
timer.Destroy(“timer”)
timer.Destroy(“HealthRegen”)
end
–hook.Add( “PlayerDeath”, “AUnIqUenaMe”, Explode )

local function HealthRegen()
if Player:Health() < 20 then
Player:SetHealth( Player:Health() + 1 )
end
end
[/lua]
This is what I have for my script. What it is supposed to do is after being bought, it’s supposed to

  1. Check to see if your health is less than 20, and it adds 1 to it every second if it is.
  2. It’s supposed to kill you by explosion in 4 minutes. The arguments in the function “Explode” are to prevent someone typing “kill” in console and exploding.
  3. If you become near death, you explode. The reason I don’t have it where you DO die is because the Player position would be bad since the player would be dead. In Trouble In Terrorist Town, you are created as a ragdoll, meaning it is possible for me to tie the explosion with the Ragdoll’s position. However, I do not know how the ragdoll is created in TTT, so I’m going when you have so low health. If anybody does know how to, I’d appreciate it.
  4. When bought, it posts in the player’s chat “HeartBeat Bomb Planted”. I was able to get this to work, but after changing it, it no longer does.

But my main problem is with defining the player that has the SWEP in their hand. I’ve tried LocalPlayer(), which returns with a nill value in-game. I’ve also done the function argument ply, but that has done the same as LocalPlayer(). I’ve tried self.Owner, which somewhat worked. It would give errors whenever it was actually pulled out in-game.

Again, the main problem I have is defining the player. I currently have it set up as Player = self.Owner, but, I have recently included that. I was just using self.Owner for every time that I needed to define the player, instead of having the variable of self.Owner = Player.

Any help is appreciated.

NOTE: In Trouble In Terrorist Town, the SWEP consists of one LUA file. It’s shared.lua, which works for both server / client. (Of course, it’s shared.lua)

TL;DR
I’m having problems with this part of the script of the SWEP.
[lua]function SWEP:Initialize()
timer.Create(“timer”, 240, 1, Explode, ply1, ent, killer )
timer.Create(“HealthRegen”, 1, 0, HealthRegen)
HealthRegen()
self.Owner:ChatPrint(“HeartBeatBomb Planted on Yourself!”)
if self.Owner:Health < 10 then
function Explode( ply1, ent, killer )
end
end

function Explode( ply1, ent, killer )
if ply1 ~= killer then
local explode = ents.Create( “env_explosion” )
explode:SetPos( Player:pos() )
explode:SetOwner ( Player() )
explode:Spawn()
explode:SetKeyValue( “iMagnitude”, “270” )
explode:Fire( “Explode”, 0, 0 )
explode:EmitSound( “weapon_AWP.Single”, 500, 500 )
end
timer.Destroy(“timer”)
timer.Destroy(“HealthRegen”)
end
–hook.Add( “PlayerDeath”, “AUnIqUenaMe”, Explode )

local function HealthRegen()
if Player:Health() < 20 then
Player:SetHealth( Player:Health() + 1 )
end
end
[/lua]

Replace “Player” with self.Owner :slight_smile:

I’ve already tried that. I get an error message of:
Tried to index global ‘self’ a nil value.

That hasn’t worked for me. I may be missing something to allow self to work properly?

You have to include the function as SWEP:ShootShit() then you can use self.Owner. Pretty sure that’s how it works.

Noob question here; This is my first SWEP.
Is it possible to put a function in a function?
(Probably not.)
I read of a possible (self) argument after a function.
I’ll try that, and try your method. I’ll have to scratch a lot off though.

Alright, I was able to fix it with your information. Thanks!