• Serverside vs Clientside functions
    6 replies, posted
Hi, so I've been writing a UI to select which prop your gun shoots, but for some reason my UI code is running serverside, not clientside. Here is the error: [CODE] [ERROR] addons/chairlauncher/lua/weapons/chairgun.lua:94: attempt to index global 'vgui' (a nil value) 1. v - addons/chairlauncher/lua/weapons/chairgun.lua:94 2. unknown - lua/includes/modules/hook.lua:84 [/CODE] My problems mostly relate to the function ChairGunUI() and the hook.Add below it, but I am posting my entire code just in case. [CODE] SWEP.PrintName = "Chair Gun" SWEP.Author = "Kim Jong Bitch" SWEP.Instructions = "LMB: Single fire, RMB: Automatic" //Spawn Info SWEP.Spawnable = true SWEP.AdminOnly = false //Ammo Info //Primary Ammo SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" //Secondary Ammo SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = true SWEP.Secondary.Ammo = "none" SWEP.Weight = 5 SWEP.AutoSwitchTo = true SWEP.AutoSwitchFrom = true SWEP.Slot = 1 SWEP.SlotPos = 1 SWEP.DrawAmmo = false SWEP.DrawCrosshair = true //models SWEP.ViewModel = "models/weapons/v_pistol.mdl" SWEP.WorldModel = "models/weapons/w_pistol.mdl" //The Model to shoot with. !!THIS IS A GLOBAL VARIABLE!! ShootModel = "models/props_combine/combine_train02a.mdl" //Precache Sounds - Experiment with this local ShootSound = Sound("Metal.SawBladeStick") function SWEP:PrimaryAttack() //Rate of fire self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 ) //Call 'ThrowChair' self:ThrowModel( ShootModel ) end function SWEP:SecondaryAttack() self:ThrowModel( ShootModel ) self.Weapon:SetNextSecondaryFire( CurTime() + 0.1) end function SWEP:ThrowModel( model_file ) self:EmitSound( ShootSound ) //Play Precached Sounds if ( CLIENT ) then return end //If you are the client then STOP //Creates prop_physics entity --local ent = ents.Create( "prop_ragdoll" ) local ent = ents.Create( "prop_physics" ) //Set the entity's model_file ent:SetModel( model_file ) //Set ent spawn pos ent:SetPos( self.Owner:EyePos() + (self.Owner:GetAimVector() * 16) ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() //Get Physics obj local phys = ent:GetPhysicsObject() if ( !IsValid( phys ) ) then ent:Remove() return end //Apply Force local velocity = self.Owner:GetAimVector() velocity = velocity * 100000000 velocity = velocity + ( VectorRand() * 10 ) --Random element? phys:ApplyForceCenter( velocity ) //Undo list cleanup.Add( self.Owner, "props", ent ) undo.Create( "that_fucking_bitch." ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() //Prevents the game from getting overwhelmed by props end /*GUI Elements*/ function ChairGunUI() //Creates a canvas to work on print(CLIENT) local ChairGunMenu = vgui.Create("DFrame") ChairGunMenu:SetPos(1920, 1080) ChairGunMenu:SetSize(300,150) ChairGunMenu:SetTitle("Chairgun Settings") ChairGunMenu:SetVisible(true) ChairGunMenu:SetDraggable(true) ChairGunMenu:ShowCloseButton(true) ChairGunMenu:MakePopup() /*Textbox for choosing your model */ local ModelNameInput = vgui.Create("DTextEntry", ChairGunMenu) ModelNameInput:SetPos(25, 50) ModelNameInput:SetSize(75,85) ModelNameInput:SetText(ShootModel) ModelNameInput.OnEnter = function(self) ShootModel = self:GetValue() end end hook.Add( "PlayerSay", "/chairgunmenu", ChairGunUI)[/CODE]
network it to your client with the net libraries, place all your UI code in cl_init inside of a netstream.Recieve function.
Use 'if SERVER then' for sv code and 'if CLIENT then' for cl code and leave anything you want shared outside of those.
[QUOTE=Pigsy;48229374]Use 'if SERVER then' for sv code and 'if CLIENT then' for cl code and leave anything you want shared outside of those.[/QUOTE] I've tried that, but the function ONLY executes as server. If I add that the code does not execute at all. [editline]17th July 2015[/editline] [QUOTE=Pigsy;48229374]Use 'if SERVER then' for sv code and 'if CLIENT then' for cl code and leave anything you want shared outside of those.[/QUOTE] I'm sorry, I'm kind of a beginner. How would I do that?
[QUOTE=Kim Jong Bitc;48229458]I've tried that, but the function ONLY executes as server. If I add that the code does not execute at all. [editline]17th July 2015[/editline] I'm sorry, I'm kind of a beginner. How would I do that?[/QUOTE] [QUOTE=ExodusTheFirs;48229192]network it to your client with the net libraries, place all your UI code in cl_init inside of a netstream.Recieve function.[/QUOTE] This guy already answered u what to do, just use google
Do as Exodus said. In your weapons folder create a weapon_classname folder then in that folder create a: init.lua, cl_init.lua and a shared.lua In init.lua [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/AddCSLuaFile]Global.AddCSLuaFile[/url] your shared and cl_init.lua. Then in init.lua call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/include]Global.include[/url] on the shared.lua. And then in cl_init.lua you need to include the shared.lua. Now init.lua only runs serverside code; cl_init.lua only runs clientside code; shared.lua is ran in both states.
I really don't think that's the right way to use the PlayerSay hook. Also PlayerSay is a serverside hook, so you'd need to either use OnPlayerChat or network the data between the server and client.
Sorry, you need to Log In to post a reply to this thread.