• Clientside and serverside self:SetWeaponHoldType
    11 replies, posted
Okay I have a Cinema server and I let players buy swords just for style with pointshop. I want to have a feature where left clicking sets your SetWeaponHoldType to "normal" and right clicking sets it to "melee2". This is what I've tried but the stances are only set for the player itself, meaning only the player changing stance sees the modification, other players only see the SetWeaponHoldType that SWEP:Initialize() has set. [CODE] function SWEP:Initialize() if SERVER then self:SetWeaponHoldType( "melee2" ) end if CLIENT then self:SetWeaponHoldType( "melee2" ) end end function SWEP:PrimaryAttack() if SERVER then self:SetWeaponHoldType( "normal" ) end if CLIENT then self:SetWeaponHoldType( "normal" ) end end function SWEP:SecondaryAttack() if SERVER then self:SetWeaponHoldType( "melee2" ) end if CLIENT then self:SetWeaponHoldType( "melee2" ) end end [/CODE] If anyone could tell me how to fix this so that everyone sees the same stance, I would be very grateful, thank you.
You should send net message to other players and set the holdtype from net.Receive.
Would you please explain to me how this would work in my situation please? I've never used net messages before.
[code]if SERVER then util.AddNetworkString("mymessage") end function SWEP:PrimaryAttack() if CLIENT then return end self:SetWeaponHoldType( "normal" ) net.Start("mymessage") net.WriteString( "normal" ) net.WriteEntity(self) net.Broadcast() end[/code] You can figure out the rest. I am not going to give you finished solution, you won't learn from it. [url]http://wiki.garrysmod.com/page/Net_Library_Usage[/url]
Thank you! I will try to script it and test until I get it. [editline]18th January 2014[/editline] So this is what I'm stuck with : [CODE]function SWEP:PrimaryAttack() if CLIENT then self:SetWeaponHoldType( "normal" ) net.Receive( "stance", function() str = net.ReadString() self:SetWeaponHoldType(str) end) end if SERVER then self:SetWeaponHoldType( "normal" ) net.Start("stance") net.WriteString( "normal" ) net.WriteEntity(self) net.Broadcast() end end [/CODE] What happens with this is that when players change stance, it changes the stance of everyone else but still no one actually sees other's stances. I will continue to test stuff but some help would really be appreciated.
I wrote half of the code and you still manage to fuck it up? Why did you edit my code? You are supposed to write a clientside net.Receive without changing the code I already posted.
[QUOTE=Robotboy655;43589055]I wrote half of the code and you still manage to fuck it up? Why did you edit my code? You are supposed to write a clientside net.Receive without changing the code I already posted.[/QUOTE] [CODE] if SERVER then self:SetWeaponHoldType( "normal" ) net.Start("stance") net.WriteString( "normal" ) net.WriteEntity(self) net.Broadcast() end end [/CODE] That looks the exact same as what you posted, and he wrote the most intuitive net.Receive, certianly what I would have tried first. He didn't include the util.AddNetworkstring, to to have that in the primary fire function would be silly. I guess I'm just posting to defend the guy. Every beginner writes code that ends up looking silly to an experienced coder such as yourself. I know I certainly do.
The part that's really confusing me is [CODE]if CLIENT then return end[/CODE] Because if I'm supposed to write a clientside net.Receive, isn't that line going to mess it up? And yes I'm fairly new at glua, my experience in days can still be counted one one hand. This is what I most recently tried : [CODE] if SERVER then util.AddNetworkString("stance") end function SWEP:PrimaryAttack() if CLIENT then return end self:SetWeaponHoldType( "normal" ) net.Start("stance") net.WriteString( "normal" ) net.WriteEntity(self) net.Broadcast() if CLIENT then net.Receive("stance", function(len) self:SetWeaponHoldType(net.ReadString()) end) end end [/CODE] But I get this error : [CODE] [ERROR] addons/frostmourne swep/lua/weapons/frostmourne/shared.lua:43: attempt to call method 'SetWeaponHoldType' (a nil value) 1. func - addons/frostmourne swep/lua/weapons/frostmourne/shared.lua:43 2. unknown - lua/includes/modules/net.lua:31 [/CODE]
You have to put net.Receive outside of all functions. That's why you send the weapon itself after the holdtype.
I'm really feeling stupid and you'll probably get mad at me because it's probably obvious or something but yeah, I'll ask anyway because I'm getting tired. I'm getting the error "Attempt to call field 'SetWeaponHoldType' (a nil value)" Here is the code I'm using atm. [CODE] function SWEP:PrimaryAttack() if CLIENT then return end self:SetWeaponHoldType( "normal" ) net.Start("stance") net.WriteString( "normal" ) net.WriteEntity(self) net.Broadcast() end if CLIENT then net.Receive("stance", function() net.ReadEntity():SetWeaponHoldType(net.ReadString()) end) end [/CODE]
[QUOTE=AlphaBarney;43589465]I'm really feeling stupid and you'll probably get mad at me because it's probably obvious or something but yeah, I'll ask anyway because I'm getting tired. I'm getting the error "Attempt to call field 'SetWeaponHoldType' (a nil value)" Here is the code I'm using atm. [CODE] function SWEP:PrimaryAttack() if CLIENT then return end self:SetWeaponHoldType( "normal" ) net.Start("stance") net.WriteString( "normal" ) net.WriteEntity(self) net.Broadcast() end if CLIENT then net.Receive("stance", function() net.ReadEntity():SetWeaponHoldType(net.ReadString()) end) end [/CODE][/QUOTE] You're very close. You are reading them in a different order to what you are writing them in the message. In the message you are writing the string then the entity, but when you are reading it you are reading the entity then the string. You need to change the order of one of those.
Finally!!! Thank you so much. It worked.
Sorry, you need to Log In to post a reply to this thread.