SWEP:Deploy not called on client?

I have some code I’d like to be executed on both Client and Server. I put the following into SWEP:Deploy()


    
if SERVER then
self.Owner:PrintMessage(3, "Server :" .. self.xhair.loss)
end
if CLIENT then
self.Owner:PrintMessage(3, "Client :" .. self.xhair.loss)
end


And yet only the Server part is ever printed.

What I want to do is updated a variable on the client when they draw their weapon. But I seem to be unable to. What is the workaround?

Maybe just forget about the whole self:Deploy() thing and do something along the lines of this

[lua]function WeaponThink()
for k, ply in pairs(player.GetAll()) do
– the players weapon
local weapon = ply:GetActiveWeapon()
–if player has weapon swep1 then give them a variable :slight_smile:
if (weapon:GetClass() == “swep1”) then
ply:SetNWInt(“name”, myvariable1);
elseif (weapon:GetClass() == “swep2” then
ply:SetNWInt(“name1”, myvariable2);

lolmaybe
end
end
end
hook.Add(“Think”, “PlayerThink”, WeaponThink)[/lua]

Edit:
Oh wait hold on me thinks this still be serverside D:

Edit:

[lua]if CLIENT then
function WeaponThink()
– the players weapon
for k, v in pairs(player.GetAll()) do
local weapon = LocalPlayer():GetActiveWeapon()
–if player has weapon swep1 then give them a variable :slight_smile:
if (weapon:GetClass() == “swep1”) then
ply:SetNWInt(“name”, myvariable1);
elseif (weapon:GetClass() == “swep2” then
ply:SetNWInt(“name1”, myvariable2);

lolmaybe
end
end
end
hook.Add(“Think”, “PlayerThink”, WeaponThink)
end[/lua]

That’s a nice idea but I was under the impression networked variables are inefficient?

Basically what I want to do is make it so that the weapon’s crosshair is set to its maximum size when equipped. So I want to do is set the crosshair variable to its maximum size and then let the client’s interpolation take care of it so I’m not constantly syncing the crosshair per frame.

So what I’m doing now is using SendLua to set a global variable, TSE_Crosshair_Bloom to true and then letting the client set that back to false.

I haven’t managed to get usermessages to work from inside a weapon and I don’t want to use networked variables because they’re inefficient (they’re requested per frame, right?) and I’d need a way for the client to then unset it.

You’re right about that, especially if you’re setting a NW var every frame like what Steel’s code does.

Use the net library instead.

So you’d put


net.Start("UniqueMessageID")
net.WriteString( self.xhair.loss )
net.Send( self.Owner )


in your Deploy function which is run on the server, and on the client you’d put something like


net.Receive( "UniqueMessageID", function()
    local string = net.ReadString()
    LocalPlayer().PrintMessage(3, string )
end) 

Be sure you run util.AddNetworkString(“UniqueMessageID”) in the init function of your swep/sent/whatever.

Of course “UniqueMessageID” can be any string.

This still has the issue of updating self.xhair.loss. That function doesn’t have scope on the weapon itself and so it can’t update the weapon’s .xhair.loss variable.

[lua]return true[/lua]

I had this issue a while ago and found it was not documented at all.
Test the weapon in multiplayer mode, not singleplayer then it should work fine.

Here’s a workaround for SinglePlayer:


if ( game.SinglePlayer() ) then self:CallOnClient( "Deploy", "" ) end