Pointshop ITEM:THINK problem

Hey what am i doing wrong here?



ITEM.Name = 'Jump Pack'
ITEM.Price = 1000
ITEM.Model = 'models/xqm/jetengine.mdl'
ITEM.Bone = 'ValveBiped.Bip01_Spine2'

function ITEM:OnEquip(ply, modifications)
	ply:PS_AddClientsideModel(self.ID)
end

function ITEM:OnHolster(ply)
	ply:PS_RemoveClientsideModel(self.ID)
end

function ITEM:ModifyClientsideModel(ply, model, pos, ang)
	model:SetModelScale(0.5, 0)
	pos = pos + (ang:Right() * 7) + (ang:Forward() * 6)
	
	return model, pos, ang
end
fuelmax = 100
 fuel = fuelmax
function ITEM:Think(ply, modifications)	
	if (ply:KeyDown( IN_FORWARD ) ) && (fuel > 0) && (ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:GetAimVector() * 100 )
			fuel = fuel - 10
	 end
	 if ( ply:KeyDown( IN_BACK ) ) && (fuel > 0) && ( ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:GetAimVector() * -1 * 100 )
			fuel = fuel - 10
	end
	if ( ply:KeyDown( IN_MOVELEFT ) ) && (fuel > 0) && ( ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity((ply:EyeAngles():Right() * -1) * 100 )
			fuel = fuel - 10
	end
	if ( ply:KeyDown( IN_MOVERIGHT ) ) && (fuel > 0) && (ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:EyeAngles():Right() * 100 )
			fuel = fuel - 10
	end
	 if (fuel < fuelmax) && !(ply:KeyDown( IN_JUMP )) then
		fuel = fuel + 0.5	
	end
end


It just doesn’t do anything

What you are doing wrong is using a single variable for fuel for all players.

You’re using a global variable for fuel, so anyone using this item will use the global amount of fuel available.

Suggest you do ply.JumpPackFuel = 100 in the OnEquip or OnBuy function and then replace all instances of the fuel variable in the Think function with ply.JumpPackFuel, and remove the fuel variable.

[editline]14th November 2014[/editline]

Give me lates.

No, you gave a more detailed answer.



ITEM.Name = 'Jump Pack'
ITEM.Price = 1000
ITEM.Model = 'models/xqm/jetengine.mdl'
ITEM.Bone = 'ValveBiped.Bip01_Spine2'

function ITEM:OnEquip(ply, modifications)
	ply:PS_AddClientsideModel(self.ID)
	 ply.fuelmax = 100
	 ply.fuel = ply.fuelmax
end

function ITEM:OnHolster(ply)
	ply:PS_RemoveClientsideModel(self.ID)
end

function ITEM:ModifyClientsideModel(ply, model, pos, ang)
	model:SetModelScale(0.5, 0)
	pos = pos + (ang:Right() * 7) + (ang:Forward() * 6)
	
	return model, pos, ang
end

function ITEM:Think(ply, modifications)	
	if (ply:KeyDown( IN_FORWARD ) ) && (ply.fuel > 0) && (ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:GetAimVector() * 100 )
			ply.fuel = ply.fuel - 10
	 end
	 if ( ply:KeyDown( IN_BACK ) ) && (ply.fuel > 0) && ( ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:GetAimVector() * -1 * 100 )
			ply.fuel = ply.fuel - 10
	end
	if ( ply:KeyDown( IN_MOVELEFT ) ) && (ply.fuel > 0) && ( ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity((ply:EyeAngles():Right() * -1) * 100 )
			ply.fuel = ply.fuel - 10
	end
	if ( ply:KeyDown( IN_MOVERIGHT ) ) && (ply.fuel > 0) && (ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:EyeAngles():Right() * 100 )
			ply.fuel = ply.fuel - 10
	end
	 if (ply.fuel < ply.fuelmax) && !(ply:KeyDown( IN_JUMP )) then
		ply.fuel = ply.fuel + 0.5	
	end
end


Is this what you meant and thanks for helping
It still doesn’t work this is on TTT by the way

What about it doesn’t work? Put in some print()s and see what code is being executed what what code is not.

Ok managed to get it to work but im getting this error

[ERROR] addons/pointshop-master/lua/pointshop/items/accessories/boosterpack.lua:40: attempt to perform arithmetic on field ‘fuel’ (a nil value)

  1. unknown - addons/pointshop-master/lua/pointshop/items/accessories/boosterpack.lua:40
  2. fn - addons/pointshop-master/lua/pointshop/sh_init.lua:146
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

fuel it’s not initialized yet
Before of working on fuel property, you must initialize it like (ply.fuel or 0) because when fuel it’s nil, this will add x to 0 due fuel it’s not initialized, anyway, you can

[lua]if ply.fuel == nil then ply.fuel = 100 end[/lua]

or

[lua]ply.fuel = (ply.fuel or 100) - x[/lua]

Remember that think it’s called 66 times (By default) on server, 10 it’s too much, use 0.125 or something like that

I tried that and i also just tried setting it everytime to see if that would fix it it still wont work




nil

[ERROR] addons/pointshop-master/lua/pointshop/items/accessories/boosterpack.lua:43: attempt to compare number with nil
  1. unknown - addons/pointshop-master/lua/pointshop/items/accessories/boosterpack.lua:43
   2. fn - addons/pointshop-master/lua/pointshop/sh_init.lua:146
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183



the code



ITEM.Name = 'Booster Pack!'
ITEM.Price = 975
ITEM.Model = 'models/xqm/jetengine.mdl'
ITEM.Bone = 'ValveBiped.Bip01_Spine2'

function ITEM:OnEquip(ply, modifications)
	ply:PS_AddClientsideModel(self.ID)
	ply.fuelmax = 50
	ply.fuel = ply.fuelmax

end

function ITEM:OnHolster(ply)
	ply:PS_RemoveClientsideModel(self.ID)
end

function ITEM:ModifyClientsideModel(ply, model, pos, ang)
	model:SetModelScale(0.5, 0)
	pos = pos + (ang:Right() * 7) + (ang:Forward() * 6)
	
	return model, pos, ang
end

function ITEM:Think(ply, modifications)
	if ply.fuel == nil then ply.fuel = 50 end --i tried to just set it  here still didn't work as in just ply.fuel = 50
	print(fuel)
	if (ply:KeyDown( IN_FORWARD ) ) && (ply.fuel > 10) && (ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:GetAimVector() * 100 )
			ply.fuel = ply.fuel - 10
	 end
	 if ( ply:KeyDown( IN_BACK ) ) && (ply.fuel > 10) && ( ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:GetAimVector() * -1 * 100 )
			ply.fuel = ply.fuel - 10
	end
	if ( ply:KeyDown( IN_MOVELEFT ) ) && (ply.fuel > 10) && ( ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity((ply:EyeAngles():Right() * -1) * 100 )
			ply.fuel = ply.fuel - 10
	end
	if ( ply:KeyDown( IN_MOVERIGHT ) ) && (ply.fuel > 10) && (ply:KeyDown( IN_JUMP ) )then
			ply:SetVelocity(ply:EyeAngles():Right() * 100 )
			ply.fuel = ply.fuel - 10
	end
	 if (ply.fuel < ply.fuelmax) && !(ply:KeyDown( IN_JUMP )) then
		ply.fuel = ply.fuel + 0.5
	end



I’d check if the player is alive before running code in the think function.

The ITEM:Think function and the ITEM:OnEquip function are not in the same realm. ITEM:OnEquip is serverside and ITEM:Think is clientside.
You cannot set a ply.fuel serverside and expect the client can read it clientside. You could use network variables, but that would be stupid to use in a think function. So you would probably want to make your own think hook serverside.

Also the way your think function is set up, you will be out of fuel in less than a second.

Any hook functions are called in any available realms. At most, he should be doing an if CLIENT then return end in the think functions.

Also the hook functions are only called if the player has the item equipped.

So the Think function is SHARED? If so he should probably create a boolean variable in the ITEM:OnEquip to set it to true and if the player leaves or unequips it then set it to true, and it should be a self variable like self.IsEquipped

Oh, nevermind I misread that.

So he should probably add a if SERVER then in the think function or if CLIENT then return end as you said. Either works.

[lua]
ITEM.Name = ‘Booster Pack!’
ITEM.Price = 975
ITEM.Model = ‘models/xqm/jetengine.mdl’
ITEM.Bone = ‘ValveBiped.Bip01_Spine2’

function ITEM:OnEquip(ply, modifications)
ply:PS_AddClientsideModel(self.ID)
ply.JumpPackFuel = 100
ply.JumpPackMaxFuel = 100
end

function ITEM:OnHolster(ply)
ply:PS_RemoveClientsideModel(self.ID)
end

function ITEM:ModifyClientsideModel(ply, model, pos, ang)
model:SetModelScale(0.5, 0)
pos = pos + (ang:Right() * 7) + (ang:Forward() * 6)

return model, pos, ang

end

function ITEM:Think(ply, modifications)
if not ply.JumpPackFuel then
ply.JumpPackFuel = 100
ply.JumpPackMaxFuel = 100
end

if ply:KeyDown(IN_FORWARD) and ply.JumpPackFuel &gt; 10 and ply:KeyDown(IN_JUMP) then
		if SERVER then ply:SetVelocity(ply:GetAimVector() * 50 ) end
		
		ply.JumpPackFuel = ply.JumpPackFuel - 5
end

if ply:KeyDown(IN_BACK) and ply.JumpPackFuel &gt; 10 and ply:KeyDown(IN_JUMP) then
		if SERVER then ply:SetVelocity(ply:GetAimVector() * -1 * 50) end
		
		ply.JumpPackFuel = ply.JumpPackFuel - 5
end

if ply:KeyDown(IN_MOVELEFT) and ply.JumpPackFuel &gt; 10 and ply:KeyDown(IN_JUMP) then
		if SERVER then ply:SetVelocity((ply:EyeAngles():Right() * -1) * 50) end
		
		ply.JumpPackFuel = ply.JumpPackFuel - 5
end

if ply:KeyDown(IN_MOVERIGHT) and ply.JumpPackFuel &gt; 10 and ply:KeyDown(IN_JUMP) then
		if SERVER then ply:SetVelocity(ply:EyeAngles():Right() * 50) end
		
		ply.JumpPackFuel = ply.JumpPackFuel - 5
end

if ply.JumpPackFuel &lt; ply.JumpPackMaxFuel and not ply:KeyDown(IN_JUMP) then
	ply.JumpPackFuel = ply.JumpPackFuel + 0.5
end

end

function ITEM:HUDPaint(ply, modifications)
if ply.JumpPackFuel then
draw.DrawText(ply.JumpPackFuel, “TargetID”, ScrW() * 0.5, ScrH() * 0.25, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
end
end
[/lua]

This works. And I added a counter for the fuel left, you can tidy it up yourself if you want to keep it.

Thanks so much for all the help