ply nil error?

I’ve started work on a jetpack’esque SENT and hit an error I’m rather surprised by:



(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)


I beleive 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]

Solution: it was miscapitalisation of IsOnGround()

As soon as you call:



	ent:Spawn()
	ent:Activate()


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.

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]

that seems to have fixed the client side problem. still having trouble with the isOnGround though

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]

thanks. that’s fixed the error!