attempt to index global 'tr' ( a nil value) Help please.

So I’ve been working on this private SWEP for my brother and I. He knows how to Lua code, so I asked him how I could get it so when I click LeftMouse, it will make the random Bieber saying (working). It worked, but then he said I should do a tracer so when I point at someone, they will get killed so it will be comical. I get this same error everytime in console.

“attempt to index global ‘tr’ (a nil value)”

Here is the code:


if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
resource.AddFile("sound/bieber1.wav")
resource.AddFile("sound/bieber2.wav")
resource.AddFile("sound/bieber3.wav")
resource.AddFile("sound/bieber4.wav")
resource.AddFile("sound/bieber5.wav")
resource.AddFile("sound/bieber6.wav")
resource.AddFile("sound/bieber7.wav")
resource.AddFile("sound/bieber8.wav")
resource.AddFile("sound/bieber9.wav")
resource.AddFile("sound/bieber10.wav")
end

SWEP.PrintName				= "Justin Bieber SWEP"
SWEP.Author					= "ME"
SWEP.Slot					= 4
SWEP.SlotPos				= 4
SWEP.Category				= "My SWEPS"
SWEP.Spawnable				= true
SWEP.AdminSpawnable			= true

SWEP.ViewModel				= "models/weapons/v_hands.mdl"

SWEP.Primary.Delay			= 3
SWEP.Primary.ClipSize		= -1		
SWEP.Primary.DefaultClip	                  = -1
SWEP.Primary.Automatic		= true	
SWEP.Primary.Ammo		= "none"	

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.Weight					= 5
SWEP.AutoSwitchTo			= false
SWEP.AutoSwitchFrom			= false

function SWEP:Think()
end

SWEP.HoldType = "normal"
function SWEP:Initialize()
	self:SetWeaponHoldType( self.HoldType )
end

function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
self.Weapon:EmitSound( "bieber/bieber" .. math.random(1,10) .. ".wav" )
if self.Owner:IsOnGround() and IsValid(self.Owner) then 
		if SERVER then
			if IsValid(self.Owner) then
				local tr = self.Owner:GetEyeTrace()
timer.Create("shootjb", 3, 2, function()
					local effectdata = EffectData()
					local vPos = self.Owner:GetShootPos()
					effectdata:SetOrigin( vPos )

					if tr.Entity:IsPlayer() or tr.Entity:IsNPC() then 
						tr.Entity:TakeDamage(tr.Entity:Health())ht
					end
end



It would be awesome if someone can help. Thanks. Incase you’re wondering, its a swep where it emits random justin bieber sayings.

Try removing local from ‘tr’, I am curious if it’ll work :wink:

Nope, gives me the same error.

I have the same problem. It worked for me when I saved the lua file again while the server was running. It must be a problem with getting the player while server is trying to start :S

Try moving the local tr after the timer.create

snip, that was retarded.

Try moving local tr = self.Owner:GetEyeTrace() into the timer function itself

Im a noob with LUA, how would I do that?

[editline]26th May 2014[/editline]


timer.Create("shootjb", 2, 1, function()local tr = self.Owner:GetEyeTrace()
					local effectdata = EffectData()
					local vPos = self.Owner:GetShootPos()
					effectdata:SetOrigin( vPos )



would this be correct?

I haven’t come across an error like this, and your code is sound. However Lua has a knack of deleting objects that has no references to it to cut down on memory ( a process named Garbage Collection ). It will probably be deleted after PrimaryAttack has finished, before the timer is called. I could be wrong, but other suggestions hasn’t worked.

Try making a ‘local tr = {}’ at the beginning of the lua file, and updating it via ‘tr = self.Owner:GetEyeTrace()’.

And as the user posed above, that would work too.

Try this, untested, I’m still pretty new to lua, but it should be working.
[lua]timer.Create(“shootjb”, 3, 2, function()
local tr = self.Owner:GetEyeTrace()
local effectdata = EffectData()
local vPos = self.Owner:GetShootPos()
effectdata:SetOrigin( vPos )

				if tr.Entity:IsPlayer() or tr.Entity:IsNPC() then 
					tr.Entity:TakeDamage(tr.Entity:Health())
					end
				end)

[/lua]

Thanks, that did work, but I’m getting a new error that I got before.

[JB TEST SWEP] lua/weapons/weapon_jb/shared.lua:67: ‘end’ expected (to close ‘if’ at line 57 near <eof>.)"

Give me a moment. If you wish, add me on Steam.

[editline]26th May 2014[/editline]

[lua] if ( SERVER ) then

AddCSLuaFile( "shared.lua" )

resource.AddFile(“sound/bieber1.wav”)
resource.AddFile(“sound/bieber2.wav”)
resource.AddFile(“sound/bieber3.wav”)
resource.AddFile(“sound/bieber4.wav”)
resource.AddFile(“sound/bieber5.wav”)
resource.AddFile(“sound/bieber6.wav”)
resource.AddFile(“sound/bieber7.wav”)
resource.AddFile(“sound/bieber8.wav”)
resource.AddFile(“sound/bieber9.wav”)
resource.AddFile(“sound/bieber10.wav”)
end

SWEP.PrintName = “Justin Bieber SWEP”
SWEP.Author = “ME”
SWEP.Slot = 4
SWEP.SlotPos = 4
SWEP.Category = “My SWEPS”
SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.ViewModel = “models/weapons/v_hands.mdl”

SWEP.Primary.Delay = 3
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = true
SWEP.Primary.Ammo = “none”

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = “none”

SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false

function SWEP:Think()
end

SWEP.HoldType = “normal”
function SWEP:Initialize()
self:SetWeaponHoldType( self.HoldType )
end

function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
self.Weapon:EmitSound( “bieber/bieber” … math.random(1,10) … “.wav” )
if self.Owner:IsOnGround() and IsValid(self.Owner) then
if SERVER then
if IsValid(self.Owner) then

timer.Create(“shootjb”, 3, 2, function()
local tr = self.Owner:GetEyeTrace()
local effectdata = EffectData()
local vPos = self.Owner:GetShootPos()
effectdata:SetOrigin( vPos )

				if tr.Entity:IsPlayer() or tr.Entity:IsNPC() then 
					tr.Entity:TakeDamage(tr.Entity:Health())
				end
		end)
			end
end
	end
end[/lua]

This is how the whole code should look like, I presume.

EDIT: Re-edited the code, I’ve noticed you missed a few end for your functions and if statements.

Still the same error. I don’t know what’s going on with the SWEP. Thanks for helping, though.

Check the edit I’ve made in my previous post.

Still the same error.

snip, checking throught stuff


  if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
resource.AddFile("sound/bieber1.wav")
resource.AddFile("sound/bieber2.wav")
resource.AddFile("sound/bieber3.wav")
resource.AddFile("sound/bieber4.wav")
resource.AddFile("sound/bieber5.wav")
resource.AddFile("sound/bieber6.wav")
resource.AddFile("sound/bieber7.wav")
resource.AddFile("sound/bieber8.wav")
resource.AddFile("sound/bieber9.wav")
resource.AddFile("sound/bieber10.wav")
end

SWEP.PrintName				= "Justin Bieber SWEP"
SWEP.Author					= "ME"
SWEP.Slot					="keyword operator">= 4
SWEP.SlotPos				="keyword operator">= 4
SWEP.Category				= "My SWEPS"
SWEP.Spawnable				="keyword operator">= true
SWEP.AdminSpawnable			="keyword operator">= true

SWEP.ViewModel				= "models/weapons/v_hands.mdl"

SWEP.Primary.Delay			="keyword operator">= 3
SWEP.Primary.ClipSize		="keyword operator">= -1		
SWEP.Primary.DefaultClip	                  ="keyword operator">= -1
SWEP.Primary.Automatic		="keyword operator">= true	
SWEP.Primary.Ammo		= "none"	

SWEP.Secondary.ClipSize		="keyword operator">= -1
SWEP.Secondary.DefaultClip	="keyword operator">= -1
SWEP.Secondary.Automatic	="keyword operator">= false
SWEP.Secondary.Ammo			= "none"

SWEP.Weight					="keyword operator">= 5
SWEP.AutoSwitchTo			="keyword operator">= false
SWEP.AutoSwitchFrom			="keyword operator">= false

function SWEP:Think()
end

SWEP.HoldType = "normal"
function SWEP:Initialize()
	self:SetWeaponHoldType( self.HoldType )
end

function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
self.Weapon:EmitSound( "bieber/bieber" .. math.random(1,10) .. ".wav" )
if self.Owner:IsOnGround() and IsValid(self.Owner) then 
		if SERVER then
			if IsValid(self.Owner) then
				
  57 -> timer.Create("shootjb", 3, 2, function()
					local tr ="keyword operator">= self.Owner:GetEyeTrace()
					local effectdata ="keyword operator">= EffectData()
					local vPos ="keyword operator">= self.Owner:GetShootPos()
					effectdata:SetOrigin( vPos )

					if tr.Entity:IsPlayer() or tr.Entity:IsNPC() then 
						tr.Entity:TakeDamage(tr.Entity:Health())ht
					end
			end)
	67 >>			end
	end
		end
	end 

That should be the whole thing. I marked the lines for you.


if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
resource.AddFile("sound/bieber1.wav")
resource.AddFile("sound/bieber2.wav")
resource.AddFile("sound/bieber3.wav")
resource.AddFile("sound/bieber4.wav")
resource.AddFile("sound/bieber5.wav")
resource.AddFile("sound/bieber6.wav")
resource.AddFile("sound/bieber7.wav")
resource.AddFile("sound/bieber8.wav")
resource.AddFile("sound/bieber9.wav")
resource.AddFile("sound/bieber10.wav")
end

SWEP.PrintName				= "Justin Bieber SWEP"
SWEP.Author					= "ME"
SWEP.Slot					= 4
SWEP.SlotPos				= 4
SWEP.Category				= "My SWEPS"
SWEP.Spawnable				= true
SWEP.AdminSpawnable			= true

SWEP.ViewModel				= "models/weapons/v_hands.mdl"

SWEP.Primary.Delay			= 3
SWEP.Primary.ClipSize		= -1		
SWEP.Primary.DefaultClip	                  = -1
SWEP.Primary.Automatic		= true	
SWEP.Primary.Ammo		= "none"	

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.Weight					= 5
SWEP.AutoSwitchTo			= false
SWEP.AutoSwitchFrom			= false

function SWEP:Think()
end

SWEP.HoldType = "normal"
function SWEP:Initialize()
	self:SetWeaponHoldType( self.HoldType )
end

function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
self.Weapon:EmitSound( "bieber/bieber" .. math.random(1,10) .. ".wav" )
if self.Owner:IsOnGround() and IsValid(self.Owner) then 
		if SERVER then
		
timer.Create("shootjb", 3, 2, function()
					if IsValid(self.Owner) then
					local tr = self.Owner:GetEyeTrace()
					local effectdata = EffectData()
					local vPos = self.Owner:GetShootPos()
					effectdata:SetOrigin( vPos )

					if tr.Entity:IsPlayer() or tr.Entity:IsNPC() then 
						tr.Entity:TakeDamage(tr.Entity:Health())
					end
						end
end)
		end
		end
end 

PS: Am I the only one seeing the annoying keyword operator things in the code?

Forum bug

Thanks for that, as for the ht, I’ve been removing it around everytime I repost the code :stuck_out_tongue:

Same error, and no, I’m seeing those keyword operator thingies, too.