Setting Entity Variable

Hello,

I have this entity, and basically I need to set a variable for that entity but I then want to access it both clientside and serverside. I’m using it to display health. I know there is already a health function and all that but I need this to be separate for many reasons.

I’ve tried this but client just returns nil:

Serverside:



hook.Add( "PlayerSpawnedVehicle", "SetCarHealthNub", function( ply, ent )
	
	if ent:IsValid() and ent:IsVehicle() then
		ent:SetCarHealth( 100 )
	end
	
end)


Shared:



local entMeta = FindMetaTable( "Entity" )

function entMeta:SetCarHealth( number )
	self.CarHealth = number
end

function entMeta:GetCarHealth()
	return self.CarHealth
end


Client:



local speed = 0

speed = veh:GetCarHealth()

-- Other code but unnecessary to post


Thanks,
Computer600

It’s FindMetaTable( “Vehicle” ) for vehicles afaik, and I hope you do realize setting a variable on server won’t update it on server, right?

Ahh kk, thanks for that, so how would I set it for serverside and clientside?

Send the variable to all clients whenever it changes using net library, or by using Entity:SetNWInt(“myvar”,2323) or server and Entity:GetNWInt(“myvar”, 1000 ) on client.