• Attempt to index 'x' a nil value
    3 replies, posted
I am coding a war of the worlds tripod (Jeff Wayne's version) as an SNPC, the code was running well until i bumped in to a very annoying error, one of which i've been able to fix when the difinative value was alternated ( 'x' = 'end' ) But instead, the difinative value this time was 'local 'Theme'' Here is the code. [CODE]AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include('shared.lua') ENT.Model = "models/tripod.mdl" ENT.Classify = CLASS_MARTIAN ENT.Health = 7216 function ENT:Initialize() self:SetModel( self.Model ) self:SetHullType( HULL_TINY ) self:SetHullSizeNormal() self:SetSolid( SOLID_BBOX ) self:SetMoveType( MOVETYPE_STEP ) self:CapabilitiesAdd( CAP_MOVE_GROUND ) self:SetMaxYawSpeed( 300 ) self:SetHealth( self.health ) local x=3 local y=90 local z=999 local min=Vector(0-(x/2),0-(y/2),0-(z/2)) local max=Vector(x/2,y/2,z/2) self:SetCollisionBounds(min,max) end function ENT:Initialize( ent ) local Theme = self:EmitSound("npc/tripod/tTheme.mp3") Theme:Play() end function ENT:OnRemove() self:Remove() Theme:Stop() end function ENT:Attack() local effectdata = EffectData() util.Effect( "hray", effectdata ) local BexpulsionPort = self, (self:GetAttachment(self:LookupAttachment(MiniGun))) local p = ("player") local expl = ents.Create("env_explosion") local tAtkSound = self:EmitSound("npc/tripod/tripod_attack.wav") local apos = ply:GetPos() effectdata:SetStart("X") effectdata:SetEnd( apos ) effectdata:Activate() expl:SetOrigin( apos ) expl:Spawn() effectdata:Remove() timer.Create( 11, 1 ) tAtkSound:Play() end function ENT:OnDeath() local ddoll = ents.Create ("prop_ragdoll") local dpos = self:GetPos() Theme:Stop() ddoll:SetModel( self.Model ) ddoll:SetOrigin( dpos ) ddoll:Spawn() end[/CODE] And here is the error: [QUOTE]ERROR ..war of the worlds - tripod snpc/lua/entities/npc_tripod/init.lua:32: attempt to index local 'Theme' (a nil value) 1. unknown - ..war of the worlds - tripod snpc/lua/entities/npc_tripod/init.lua:32[/QUOTE] So, the amount of times i've encountered this error makes me want to smash my skull against the keyboard and smear the words "GOD, HELP ME!" in blood on the wall... If you can help then... you will be helping to create one of the tallest SNPCs in gmod history... (Not that it matters or anything) [B][U]ALSO NOTE THAT THE REASON I USED EMITSOUND FOR THE MUSICAL THEME WAS BECAUSE CREATESOUND FUCKED UP FOR ME... again...[/U][/B]
[QUOTE=Dave_Parker;38344882][lua]AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include('shared.lua') ENT.Model = "models/tripod.mdl" ENT.Classify = CLASS_MARTIAN ENT.Health = 7216 function ENT:Initialize() self:SetModel( self.Model ) self:SetHullType( HULL_TINY ) self:SetHullSizeNormal() self:SetSolid( SOLID_BBOX ) self:SetMoveType( MOVETYPE_STEP ) self:CapabilitiesAdd( CAP_MOVE_GROUND ) self:SetMaxYawSpeed( 300 ) self:SetHealth( self.health ) local x=3 local y=90 local z=999 local min=Vector(0-(x/2),0-(y/2),0-(z/2)) local max=Vector(x/2,y/2,z/2) self:SetCollisionBounds(min,max) end function ENT:Initialize( ent ) self:EmitSound("npc/tripod/tTheme.mp3") end function ENT:OnRemove() self:Remove() end function ENT:Attack() local effectdata = EffectData() util.Effect( "hray", effectdata ) local BexpulsionPort = self, (self:GetAttachment(self:LookupAttachment(MiniGun))) local p = ("player") local expl = ents.Create("env_explosion") local tAtkSound = self:EmitSound("npc/tripod/tripod_attack.wav") local apos = ply:GetPos() effectdata:SetStart("X") effectdata:SetEnd( apos ) effectdata:Activate() expl:SetOrigin( apos ) expl:Spawn() effectdata:Remove() timer.Create( 11, 1 ) tAtkSound:Play() end function ENT:OnDeath() local ddoll = ents.Create ("prop_ragdoll") local dpos = self:GetPos() ddoll:SetModel( self.Model ) ddoll:SetOrigin( dpos ) ddoll:Spawn() end[/lua] Theme was a variable local to the Initialize function, here we've set it to self (referring to the entity) so you can effectively use self.Theme in any function you call on the entity. Edited it because I didn't even read EmitSound, it stops as soon as the entity is removed either way. Also use [noparse][lua][/lua][/noparse] tags in the future as they provide line numbering. Also your timer will call a nil function after 11 seconds once. If you only have to do it once, use timer.Simple.[/QUOTE] Thanks for the fixes, although unfortunately, the SNPC's model is now an ERROR sign which is quite annoying considering there is no error to support it's fix.
Sorry, you need to Log In to post a reply to this thread.