• ply nil error?
    7 replies, posted
I've started work on a jetpack'esque SENT and hit an error I'm rather surprised by: [code] (Serverside) ...ons\qu_thrusterpack\lua\entities\jumppack\shared.lua:57: attempt to call method 'isOnGround' (a nil value) (clientside) ...ons\qu_thrusterpack\lua\entities\jumppack\shared.lua:37: attempt to index field 'ply' (a nil value) [/code] I [I]beleive[/I] it's from it not keeping ply properly. shared.lua: [lua] ENT.Type = "anim" ENT.Category = "MotionEntitys" ENT.PrintName = "JumpPack" ENT.Author = "Quarg" ENT.Contact = "Gmod@maxcd.org.uk" ENT.Purpose = "aid in long/high jumps" ENT.Instructions = "Spawn to wear, undo to unwear INCOMPLETE" ENT.Spawnable = true function ENT:SpawnFunction( ply, trace ) if ply.JumpPack then return end local ent = ents.Create( "JumpPack" ) ent:SetPos( ply:GetPos() ) ent:Spawn() ent:Activate() ent:SetParent(ply) ent:SetModel("models/props_c17/trappropeller_engine.mdl") ply.JumpPack = true ent.ply = ply ent:SetNWEntity("ply", ply ) --Fuel Stuff! ent:SetNWInt("fuel", 100) ent:SetNWInt("maxFuel", 100) ent:SetNWInt("fuelRecoverRate", 5) ent:SetNWInt("fuelUsageRate", 1) ent.fuel = 100 ent.maxFuel = 100 ent.fuelRecoverRate = 5 ent.fuelUsageRate = 1 return ent end function ENT:Think() if not self.ply.JumpPack then return end --clientside error: fixed by using "if not (self.ply and self.ply.JumpPack) then return end" local ply = self.ply if ply:KeyDown(IN_USE) and ply:KeyDown(IN_WALK) then self:Remove() --drop if holding both <alt> and <E> --May Not Work elseif ply:KeyDown(IN_JUMP) then if self.fuel >= self.fuelUsageRate then ply:SetVelocity(ply:EyeAngles():Up() * 20) self.fuel = self.fuel - self.fuelUsageRate -- use up fuel! end if not ply:isOnGround() then ply:SetAllowFullRotation(true) end else ply:SetAllowFullRotation(false) end if ply:isOnGround() then if self.fuel != self.maxfuel then self.fuel = self.fuel + self.fuelRecoverRate --Recover fuel! end end if self.fuel >= self.maxFuel then self.fuel = self.maxFuel --limit fuel! end end function ENT:OnRemove() self.ply.JumpPack = false self.ply:SetAllowFullRotation(false) end [/lua] [B]Solution:[/B] it was miscapitalisation of IsOnGround()
As soon as you call: [code] ent:Spawn() ent:Activate() [/code] The entity spawns and starts to 'think', however it doesn't set ent.ply until after the entities spawned, so in the short space of time between it spawning and setting ent.ply, it's thinking and trying to use ply, when it hasn't been set yet. Move ent:Spawn() and ent:Activate() to the end of the fuel stuff.
[QUOTE=Drakehawke;23707853]As soon as you call: [code] ent:Spawn() ent:Activate() [/code] The entity spawns and starts to 'think', however it doesn't set ent.ply until after the entities spawned, so in the short space of time between it spawning and setting ent.ply, it's thinking and trying to use ply, when it hasn't been set yet. Move ent:Spawn() and ent:Activate() to the end of the fuel stuff.[/QUOTE] *trys it* doesn't work.
function(ply) end That should work.
try [lua] if not (self.ply and self.ply.JumpPack) then return end [/lua]
[QUOTE=_nonSENSE;23716611]try [lua] if not (self.ply and self.ply.JumpPack) then return end [/lua][/QUOTE]that seems to have fixed the client side problem. still having trouble with the isOnGround though
[QUOTE=Dave_Parker;23725268]Because it's IsOnGround. This isn't E2. Also, I don't think the client has anything to do in that think function, so I guess you're better off with [lua]if(!SERVER) then return end[/lua][/QUOTE] thanks. that's fixed the error!
Sorry, you need to Log In to post a reply to this thread.