Issue with variables

Hi

I have here, in my entity:



function ENT:Initialize()
	local vehicleSpeed = 0
	Laser = Material( "cable/redlaser" )
	local maxSpeed = 180
end


And in the Draw code, I modify these, for example: maxSpeed = 123

If I spawn two of these entities, both of them reflect the update even if only one of them should do it, because the If trigger only triggers in one of them.
Why do they share variables?

EDIT:
Solution:
When you use net to send data, you need to send the Entity or the Entity index and reference it.

If you want variable to be per entity and not shared for all ents with that class you shoould use ENT.Variable instead of local variable. Like this.
[lua]
function ENT:Initialize()
self.vehicleSpeed = 0
Laser = Material( “cable/redlaser” )
self.maxSpeed = 180
end
[/lua]

I did that, but it still didn’t work (I have a cam3D2D to output these speeds).
My cam still shows shared variables, I tried to put self.variable in the cam, then they don’t change at all.

It would help if you would show us the Draw code you use (complete).
In theory it should work this way.



unction ENT:Draw()

	self:DrawModel()
	net.Receive("OpenDermaMenu", function()
		local DermaPanel = vgui.Create( "DFrame" )
		DermaPanel:SetPos( ScrW()/2-140, ScrH()/2-120 )
		DermaPanel:SetSize( 280, 120 )
		DermaPanel:SetTitle( "Configure speed checker" )
		DermaPanel:SetVisible( true )
		DermaPanel:SetDraggable( true )
		DermaPanel:ShowCloseButton( true )
		DermaPanel:MakePopup()
		
		local NumSliderThingy = vgui.Create( "DNumSlider", DermaPanel )
		NumSliderThingy:SetPos( 5,15  )
		NumSliderThingy:SetSize( 280, 100 ) -- Keep the second number at 100
		NumSliderThingy:SetText( "Max vehicle speed:" )
		NumSliderThingy:SetValue( maxSpeed )
		NumSliderThingy:SetMin( 60 ) -- Minimum number of the slider
		NumSliderThingy:SetMax( 200 ) -- Maximum number of the slider
		NumSliderThingy:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
	   local myButton=vgui.Create("DButton",DermaPanel)
	   myButton:SetText("Save")
	   myButton:SetPos( 25, 80 )
	   function myButton:DoClick()
		maxSpeed = math.Round(NumSliderThingy:GetValue()) -- self here doesnt update the cam
		end
		end)



	self:SetColor(Color(255, 255, 255, 255))
	local Pos = self:GetPos()
	local Ang = self:GetAngles()
	surface.SetFont("HUDNumber5")
	local PosTwo = Vector(Pos.x, Pos.y, Pos.z)
	local tracedata = {}
	tracedata.start = self:LocalToWorld( Vector( 0, -2.5, 58.5 ) )
	tracedata.endpos = self:LocalToWorld( Vector( 700, -2.5, 58.5 ) )
	tracedata.filter = self
 
	local trace = util.TraceLine(tracedata)
	if trace.HitNonWorld then
		target = trace.Entity
		if target:IsVehicle() then
	    local vehicleVel = target:GetVelocity():Length()
        local vehicleConv = -1
        local terminal = 0     
			terminal = math.Clamp(vehicleVel / 2000, 0, 1)
			vehicleConv = math.Round(vehicleVel / 10)
        
			speed = math.Clamp(vehicleConv, 0, 320)
			if(CLIENT) then
				vehicleSpeed = speed -- self here doesn't update cam at all
			end		
		end
	end
	
	local Vector1 = self:LocalToWorld( Vector( 0, -2.5, 58.5 ) )
	local Vector2 = self:LocalToWorld( Vector( 700, -2.5, 58.5 ) )
 
	render.SetMaterial( Laser )
	render.DrawBeam( Vector1, Vector2, 3, 1, 1, Color( 255, 0, 0, 255 ) ) 
	
	Ang:RotateAroundAxis(Ang:Up(), -90)
	Ang:RotateAroundAxis(Ang:Forward(), 7)
	local TextWidth = surface.GetTextSize(vehicleSpeed .. " MPH")
	cam.Start3D2D(Pos + Ang:Up() * 35.5, Ang, 0.1)		
	draw.WordBox(-2.5, -TextWidth*0.5, 0, vehicleSpeed .. " MPH/" .. maxSpeed .. " max", "HUDNumber5", Color(140, 0, 0, 140), Color(255,255,255,255))  -- self here doesn't update either at all
	cam.End3D2D()
end



Try this.
[lua]
function ENT:Draw()

self:DrawModel()
net.Receive("OpenDermaMenu", function()
	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( ScrW()/2-140, ScrH()/2-120 )
	DermaPanel:SetSize( 280, 120 )
	DermaPanel:SetTitle( "Configure speed checker" )
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup()
	
	local NumSliderThingy = vgui.Create( "DNumSlider", DermaPanel )
	NumSliderThingy:SetPos( 5,15  )
	NumSliderThingy:SetSize( 280, 100 ) -- Keep the second number at 100
	NumSliderThingy:SetText( "Max vehicle speed:" )
	NumSliderThingy:SetValue( self.maxSpeed )
	NumSliderThingy:SetMin( 60 ) -- Minimum number of the slider
	NumSliderThingy:SetMax( 200 ) -- Maximum number of the slider
	NumSliderThingy:SetDecimals( 0 ) -- Sets a decimal. Zero means it's a whole number
   local myButton=vgui.Create("DButton",DermaPanel)
   myButton:SetText("Save")
   myButton:SetPos( 25, 80 )
   function myButton.DoClick(button)
	self.maxSpeed = math.Round(NumSliderThingy:GetValue()) -- self here doesnt update the cam
	end
	end)



self:SetColor(Color(255, 255, 255, 255))
local Pos = self:GetPos()
local Ang = self:GetAngles()
surface.SetFont("HUDNumber5")
local PosTwo = Vector(Pos.x, Pos.y, Pos.z)
local tracedata = {}
tracedata.start = self:LocalToWorld( Vector( 0, -2.5, 58.5 ) )
tracedata.endpos = self:LocalToWorld( Vector( 700, -2.5, 58.5 ) )
tracedata.filter = self

local trace = util.TraceLine(tracedata)
if trace.HitNonWorld then
	target = trace.Entity
	if target:IsVehicle() then
    local vehicleVel = target:GetVelocity():Length()
    local vehicleConv = -1
    local terminal = 0     
		terminal = math.Clamp(vehicleVel / 2000, 0, 1)
		vehicleConv = math.Round(vehicleVel / 10)
    
		speed = math.Clamp(vehicleConv, 0, 320)
		if(CLIENT) then
			self.vehicleSpeed = speed -- self here doesn't update cam at all
		end		
	end
end

local Vector1 = self:LocalToWorld( Vector( 0, -2.5, 58.5 ) )
local Vector2 = self:LocalToWorld( Vector( 700, -2.5, 58.5 ) )

render.SetMaterial( Laser )
render.DrawBeam( Vector1, Vector2, 3, 1, 1, Color( 255, 0, 0, 255 ) ) 

Ang:RotateAroundAxis(Ang:Up(), -90)
Ang:RotateAroundAxis(Ang:Forward(), 7)
local TextWidth = surface.GetTextSize(vehicleSpeed .. " MPH")
cam.Start3D2D(Pos + Ang:Up() * 35.5, Ang, 0.1)		
draw.WordBox(-2.5, -TextWidth*0.5, 0, vehicleSpeed .. " MPH/" .. self.maxSpeed .. " max", "HUDNumber5", Color(140, 0, 0, 140), Color(255,255,255,255))  -- self here doesn't update either at all
cam.End3D2D()

end
[/lua]

I removed the:
=“keyword”>

I assume they shouldn’t be there and they gave errors.
Now the maxSpeed is updated in the camera for only the first entity I spawn, and the vehicleSpeed isn’t changed at all.

Edit:
self. was missing from some variables, I added them and it works great!
Thank you!