Save variable set number

So here is my code

and for every time I put in a crate it goes back

spawned sent crate
You added 1 crate
You have added a total of 1 crates.
You added 1 crate
You have added a total of 1 crates.
You added 1 crate
You have added a total of 1 crates.

Also the server log repeats that I have added a crate a couple times even though I added 1.

I know its due to the cratesAdded = 0 but how can I change it so it doesnt loop like that

Something looks terribly wrong with your Initialize function (the image you showed doesn’t show it actually ending)

Your problem however, is that you tried to change cratesAdded in a way that redefines it.

The term “local” is used only when defining a variable (this is when you said local cratesAdded = 0).

The “cratesAdded = cratesAdded + 1” line thinks you are defining a new localized variable, which it defines as the previously defined one (which starts at zero) plus 1. The print line then reads the most appropriate variable of the two variables both named cratesAdded (one in the entity meta scope and the other in the current calling of ENT.Touch), which will be the most localized one (the second one), and print 1.

If you remove the local from the line that adds 1, it will then refer to the variable in the meta scope at the top and work correctly.

Beautiful explanation and it works. Thanks buddy! :slight_smile:

You should also change the “local cratesAdded = 0” to “ENT.cratesAdded = 0” and call it in Touch via “self.cratesAdded = self.cratesAdded + 1”.

This is due to how entity files are read by the game. The file that you’re editing actually only ever gets read once and copies its information into a template to create the entity. If you left it as-is, it won’t work right.

Using ENT.cratesAdded instead will make a variable tied to a specific instance of the entity. Inside entity functions you refer to data stored on specific entities by referring to the entity itself as the term self.

[editline]23rd February 2016[/editline]

in other words you should ignore the post i made before because while technically true it doesn’t properly solve your problem regarding entities

You’re redeclaring cratesAdded everytime since you put local in front of it again. Also, I believe you should use an entity variable instead of a local due to multiple instances messing with the environment serverside.

[editline]23rd February 2016[/editline]

Really far Ninja’d

Alright… Done. Except I’m still getting this

You added 1 crate
You have added a total of 8 crates.
You added 1 crate
You have added a total of 9 crates.
You added 1 crate
You have added a total of 10 crates.

Even though I am adding 1! Crate.

As well as this

[ERROR] addons/citizenjobs/lua/entities/crate/init.lua:5: unexpected symbol near '.'
  1. unknown - addons/citizenjobs/lua/entities/crate/init.lua:0

Which is the ENT.cratesAdded

The touch function will run every single time it touches the vehicle, which may be more than it seems happening in a very rapid timespan. You are removing the entity though…

[editline]23rd February 2016[/editline]

show us the full code not these awful images

AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include('shared.lua')

function ENT:Initialize() self:SetModel("models/Items/item_item_crate.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self:SetUseType(SIMPLE_USE) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end end

ENT.cratesAdded = 0

function ENT:Touch(ent)
	if ent.IsVehicle() then
		self:EmitSound( "items/ammocrate_close.wav", 75, 100, 1, CHAN_AUTO )
		print("You added 1 crate")
		self.cratesAdded = self.cratesAdded + 1
		print("You have added a total of "..cratesAdded.. " crates.")

Are you sure the problem is on line 5? Your print at the end won’t work because you forgot to put the term self. infront of it.

[editline]23rd February 2016[/editline]

also when you run self:Remove() the entity doesn’t exist anymore, so adding onto a counter stored on the deleted entity makes zero sense at all

Im working on another system to get the amount of crates added and then you get paid for it. I fixed line 5

[editline]23rd February 2016[/editline]

but I get what you mean, When its removed the code runs over again. Maybe I have to network it and write to a string?