• attempt to index global 'tr' ( a nil value) Help please.
    22 replies, posted
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: [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 [/CODE] 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 ;)
[QUOTE=Netheous;44915014]Try removing local from 'tr', I am curious if it'll work ;)[/QUOTE] 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
[QUOTE=MattJeanes;44915555]Try moving local tr = self.Owner:GetEyeTrace() into the timer function itself[/QUOTE] Im a noob with LUA, how would I do that? [editline]26th May 2014[/editline] [CODE]timer.Create("shootjb", 2, 1, function()local tr = self.Owner:GetEyeTrace() local effectdata = EffectData() local vPos = self.Owner:GetShootPos() effectdata:SetOrigin( vPos ) [/CODE] 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.
[QUOTE=ChrisFiore321;44915658]Im a noob with LUA, how would I do that? [editline]26th May 2014[/editline] [CODE] code [/CODE] would this be correct?[/QUOTE] 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]
[QUOTE=Captain Spark;44915756]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][/QUOTE] 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>.)"
[QUOTE=ChrisFiore321;44915815]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>."[/QUOTE] 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.
[QUOTE=Captain Spark;44915843]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)[/lua] This is how the whole code should look like, I presume.[/QUOTE] Still the same error. I don't know what's going on with the SWEP. Thanks for helping, though.
[QUOTE=ChrisFiore321;44915905]Still the same error. I don't know what's going on with the SWEP. Thanks for helping, though.[/QUOTE] Check the edit I've made in my previous post.
[QUOTE=Captain Spark;44915915]Check the edit I've made in my previous post.[/QUOTE] Still the same error.
snip, checking throught stuff
[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 ="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 [/CODE] That should be the whole thing. I marked the lines for you.
[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 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 [/CODE] PS: Am I the only one seeing the annoying keyword operator things in the code?
[QUOTE=Captain Spark;44916047]PS: Am I the only one seeing the annoying keyword operator things in the code?[/QUOTE] Forum bug
[QUOTE=Bo98;44916059]Forum bug[/QUOTE] Thanks for that, as for the ht, I've been removing it around everytime I repost the code :P
[QUOTE=Captain Spark;44916047][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 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 [/CODE] PS: Am I the only one seeing the annoying keyword operator things in the code?[/QUOTE] Same error, and no, I'm seeing those keyword operator thingies, too.
[QUOTE=ChrisFiore321;44916070]Same error, and no, I'm seeing those keyword operator thingies, too.[/QUOTE] In that case, try playing with the "end" around here and there, I'm probably missing something really really obvious, and in that case I apologize. Just try playing around, and keep the tr, where it is. The code should work apart from the annoying end error.
[QUOTE=Captain Spark;44916091]In that case, try playing with the "end" around here and there, I'm probably missing something really really obvious, and in that case I apologize. Just try playing around, and keep the tr, where it is. The code should work apart from the annoying end error.[/QUOTE] Got rid of the end error by placing an "end" inbetween the 2 lines of code. Well you guessed it, theres another error. "Unexpected symbol near '('"
Uh, wanna add me on steam? Profile link : [url]http://steamcommunity.com/id/captainsparkly[/url] I'm doing some stuff right now, just hit me up there.
Sorry, you need to Log In to post a reply to this thread.