Server to Client

Hey! I have another stupid question about lua.
When I’m coding and I want to send a server info to the client, I have a bad habit to use the network variables like ent:SetNWInt(“Health”, health) and I know it’s lagging the server… but I don’t really know another method to send a server variable to the client!

Like for exemple a script from Ltp0wer : Simple Prop Damage.
If for exemple I want to send this value: spd[ent:EntIndex()] to the client, how am I doing this?
For me, I’ll simply use ent:SetNWInt(“Health”, spd[ent:EntIndex()]), but I know it’s not a good idea, so what I need to do?

Please, an exemple! I need to see to understand.
Thanks guys for your helps!

[lua]
spd = spd or {}

if !ConVarExists(“spd”) then
CreateConVar(“spd”, ‘0’, FCVAR_NOTIFY)
end

if !ConVarExists(“spd_fire”) then
CreateConVar(“spd_fire”, ‘2’, FCVAR_NOTIFY)
end

hook.Add(“EntityRemoved”, “spdEntityRemoved”, function(ent)
if ent:IsValid() and
ent:GetClass() == “prop_physics” and
ent:Health() == 0 then
spd[ent:EntIndex()] = nil
end
end)

hook.Add(“EntityTakeDamage”, “spdEntityTakeDamage”, function(ent, inflictor, attacker, amount)
if GetConVarNumber(“spd”) != 0 and
ent:IsValid() and
spd[ent:EntIndex()] then
spd[ent:EntIndex()] = spd[ent:EntIndex()] - amount / GetConVarNumber(“spd”)
local mass = ent:GetPhysicsObject():GetMass()
local colmod = spd[ent:EntIndex()] / mass
ent:SetColor(255, colmod * 255, colmod * 255, 255)

    if math.random(1, 100) < GetConVarNumber("spd_fire") then
        ent:Ignite(10, 20)
    end

    if spd[ent:EntIndex()] < mass * 0.5 then
        ent:GetPhysicsObject():EnableMotion(true)
    end

    if spd[ent:EntIndex()] < mass * 0.25 and
    ent:IsConstrained() then
        local effect = EffectData()
        effect:SetStart(ent:GetPos())
        effect:SetOrigin(ent:GetPos()+ Vector(0, 0, 10))
        effect:SetScale(mass)
        util.Effect("cball_explode", effect)
        constraint.RemoveAll(ent)
    end

    if spd[ent:EntIndex()] < 1 then
		local effect = EffectData()
		effect:SetStart(ent:GetPos())
		effect:SetOrigin(ent:GetPos()+ Vector(0, 0, 10))
		effect:SetScale(mass)
		util.Effect("Explosion", effect)
		ent:Remove()
    end
end

end)

hook.Add(“OnEntityCreated”, “spdOnEntityCreated”, function(ent)
timer.Simple(0.01, function()
if ent:IsValid() and
ent:GetClass() == “prop_physics” and
ent:Health() == 0 then
spd[ent:EntIndex()] = ent:GetPhysicsObject():GetMass()
end
end, ent)
end)
[/lua]

Here the link to download this little script:
http://www.garrysmod.org/img/?t=dll&id=67807

And… the thread:
http://www.facepunch.com/showthread.php?t=562270

User messages.

Ya, but I simply know a little bit about that… like how to send a little value to the client…
But NOW I need to send a value for each entity.

I want to make a HUD like this:
http://img228.imageshack.us/img228/3277/hudu.th.jpg

So when you look an entity, it shows its health.
User messages can do that? If yes, please give me an exemple because the Wiki cannot explain how…

No, use networked ints for that, that’s what they’re made for.

i think in this case it’s not that bad that you use ent:SetNWInt, look at flood gamemode code, somewhere in it i saw that you need

I’ve read that “networked variables” takes a lot of information ressource from the server, and most people don’t recommend its use, no?

Can’t you just use GetTable()?

ply:GetTable().testing = true

something like that?^^

That’s not shared…

So I guess GetNWInt and GetNWBools are the only way?

Yeah, pretty much. You could make a system which sends a message to the server when aiming at an entity, which sends you the info back.

[lua]
enthealth = LocalPlayer():GetEyeTraceNoCursor().Entity:Health()
[/lua]
?

Would only work if the is using ent:SetHealth(), but he is using his own system.