• Lua weapon help!
    5 replies, posted
Hey all, I'm trying to make my first lua weapon that is based on the chairthrower on wiki.garrysmod.com. The thing I'm trying to add is a menu where you can enter an custom prop, an a button to cleanup all of the spawned props. But it wont work, the menu won't open and I get an Lua error. Here's the code in shared.lua: [code]SWEP.Author = "Jackorobot"; SWEP.Contact = ""; SWEP.Purpose = "Title says everything!"; SWEP.Instructions = "Left click to throw an office chair; right click to throw your cohsen chair; Reload to undo Thrown Chairs; right click+reload to open the chooser menu"; SWEP.Category = "Prop Launchers" SWEP.Spawnable = true; SWEP.AdminSpawnable = true; SWEP.ViewModel = "models/weapons/v_bugbait.mdl"; SWEP.WorldModel = "models/weapons/w_bugbait.mdl"; SWEP.Primary.ClipSize = -1; SWEP.Primary.DefaultClip = -1; SWEP.Primary.Automatic = false; SWEP.Primary.Ammo = "none"; SWEP.Secondary.ClipSize = -1; SWEP.Secondary.DefaultClip = -1; SWEP.Secondary.Automatic = false; SWEP.Secondary.Ammo = "none"; local ShootSound = Sound ("Weapon_Crossbow.BoltFly"); function SWEP:Reload() Menu() end function SWEP:Think() end local secondProp="models/props_c17/FurnitureChair001a.mdl" if secondProp == "" then secondProp = "models/props_c17/FurnitureChair001a.mdl" end function Menu() local Frame = vgui.Create( "Frame" ); //Create a frame Frame:SetSize( 200, 200 ); //Set the size to 200x200 Frame:SetPos( 100, 100 ); //Move the frame to 100,100 Frame:SetVisible( true ); //Visible Frame:MakePopup( ); //Make the frame Frame:PostMessage( "SetTitle", "text", "Prop chooser menu" ); //Set the title to "This is the title" local Button = vgui.Create( "Button", Frame ); //Create a button that is attached to Frame Button:SetText( "OK" ); //Set the button's text to "Click me!" Button:SetPos( 30, 10); //Set the button's position relative to it's parent(Frame) Button:SetWide( 50 ); //Sets the width of the button you're making function Button:DoClick( ) //This is called when the button is clicked secondProp=Bar:GetValue() Frame:SetVisible(false) end local Bar = vgui.Create("DTextEntry", Frame) Bar:SetPos(30,5) Bar:SetWide(95) Bar:SetEnterAllowed(true) local Button2 = vgui.Create("Button2",Frame) Button2:SetText("Remove shot props") Button2:SetPos(90,10) Button2:SetWide(75) function Button2:DoClick() cleanup.Register("Thrown chair") Frame:SetVisible(false) end end function SWEP:Initialize() if ( SERVER ) then self:SetWeaponHoldType( self.HoldType ) end end function SWEP:throw_attack (model_file) //Get an eye trace. This basically finds out where the shot hit //This SWEP makes very little use of the trace, except to calculate //the amount of force to apply to the object to throw it. local tr = self.Owner:GetEyeTrace(); //We now make some shooting noises and effects using the sound we //loaded up earlier self.Weapon:EmitSound (ShootSound); self.BaseClass.ShootEffects (self); //We now exit if this function is not running on the server if (!SERVER) then return end; //The next task is to create a physics entity based on the supplied model. local ent = ents.Create ("prop_physics"); ent:SetModel (model_file); //Set the initial position of the object. This might need some fine tuning; but it //seems to work for the models I have tried ent:SetPos (self.Owner:EyePos() + (self.Owner:GetAimVector() * 16)); ent:SetAngles (self.Owner:EyeAngles()); ent:Spawn(); //Now we need to get the physics object for our entity so we can apply a force to it local phys = ent:GetPhysicsObject(); //Time to apply the force. My method for doing this was almost entirely empirical //and it seems to work fairly intuitively with chairs. local shot_length = tr.HitPos:Length(); phys:ApplyForceCenter (self.Owner:GetAimVector():GetNormalized() * math.pow (shot_length, 3)); //Now for the all important part of adding the spawned objects to the undo and cleanup data. cleanup.Add (self.Owner, "Chairs", ent); undo.Create ("Thrown chair"); undo.AddEntity(ent); undo.SetPlayer(self.Owner); undo.Finish(); end function SWEP:PrimaryAttack() //Call the throw attack function self:throw_attack ("models/props/cs_office/Chair_office.mdl"); end /* Throw a wooden chair on secondary attack */ function SWEP:SecondaryAttack() self:throw_attack(secondProp); end[/code] I would really apreciate help, Thanks in forward!
Bump!
When you get lua errors post them here and specify if they were yellow (clientside) or blue (serverside). You'll get a ton more help that way.
They where blue, ill fetch them later, im at school now...
I think that you should put the Derma in a "if (CLIENT) then" statement, and then add the menu to a hook, being called when you press the wanted key.
Tnx mate, that was the missing piece lol works properly right now!
Sorry, you need to Log In to post a reply to this thread.