• I'm having a problem with my first Lua script.
    9 replies, posted
Hello! I'm currently having a problem with my first Lua script. I am modifying the "Chair Throwing Gun" script of the GMOD Lua Wiki, but something is giving me errors. I get this error: [CODE][ERROR] lua/autorun/weapon_melonlauncher.lua:4: attempt to index global 'SWEP' (a nil value) 1. unknown - lua/autorun/weapon_melonlauncher.lua:4 [/CODE] Here's my code: [CODE]print ("MelonLauncher by SoraTheDev has launched sucessfully!") AddCSLuaFile("cl_init.lua") AddCSLuaFile("weapon_melonlauncher.lua") SWEP.PrintName = "Melon Launcher" SWEP.Author = "SoraTheDev" SWEP.Instructions = "Press the left mouse button to fire a melon and the right mouse button to fire a sawblade!" SWEP.Contact = "x" --------------------------- SWEP.Spawnable = true SWEP.AdminOnly = false --------------------------- 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" --------------------------- SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.Slot = 1 SWEP.SlotPos = 2 SWEP.DrawAmmo = false SWEP.DrawCrosshair = false --------------------------- SWEP.ViewModel = "models/weapons/v_pistol.mdl" SWEP.WorldModel = "models/weapons/w_pistol.mdl" --------------------------- local primarySound = Sound("garrysmod/addons/melonlauncher/lua/sounds/doot.wav") local secondarySound = Sound("garrysmod/addons/melonlauncher/lua/sounds/pootis.mp3") --------------------------- function SWEP:PrimaryAttack() self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 ) self:melon("models/props_junk/watermelon01.mdl") end function SWEP:SecondaryAttack() self:blade("models/props_forest/sawblade_moving.mdl") end function SWEP:melon(model_file) self:EmitSound(primarySound) if (CLIENT) then return end local ent = ents.Create("prop_physics") if (!IsValid (ent)) then return end ent:SetModel (model_file) ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() local phys = ent:GetPhysicsObject() if (!IsValid (phys)) then ent:Remove() return end local velocity = self.Owner:GetAimVector() velocity = velocity * 200 velocity = velocity + (VectorRand() * 10) phys:ApplyForceCenter(velocity) cleanup.Add( self.Owner, "props", ent) undo.Create("Thrown_Melon") undo.AddEntity(ent) undo.SetPlayer(self.Owner) undo.Finish() end function SWEP:blade(model_file) self:EmitSound(secondarySound) if (CLIENT) then return end local ent = ents.Create("prop_physics") if (!IsValid (ent)) then return end ent:SetModel (model_file) ent:SetPos( self.Owner:EyePos() + ( self.Owner:GetAimVector() * 16 ) ) ent:SetAngles( self.Owner:EyeAngles() ) ent:Spawn() local phys = ent:GetPhysicsObject() if (!IsValid (phys)) then ent:Remove() return end local velocity = self.Owner:GetAimVector() velocity = velocity * 200 velocity = velocity + (VectorRand() * 10) phys:ApplyForceCenter(velocity) cleanup.Add( self.Owner, "props", ent) undo.Create("Thrown_Sawblade") undo.AddEntity(ent) undo.SetPlayer(self.Owner) undo.Finish() end[/CODE] How can I solve this? Thank you!
I think you have to put weapons in lua/weapons/
[QUOTE=zoox;52067027]I think you have to put weapons in lua/weapons/[/QUOTE] I did it, but it doesn't work. I mean, I first put the script on lua/weapons/ and then I put it on garrysmod/lua/autorun.
Do you get a different error message when you put in in lua/weapons/ ?
[QUOTE=zoox;52067047]Do you get a different error message when you put in in lua/weapons/ ?[/QUOTE] As I said, I first put the script on lua/weapons/, and then I tried to use lua_openscript but it wouldn't work. So, I put it on garrysmod/lua/autorun.
If I put your code in lua/weapons/ it seems to work fine for me
You don't use lua_openscript; lua/weapons is loaded automatically in the SWEP environment -- it will not work in lua/autorun.
[QUOTE=zoox;52067065]If I put your code in lua/weapons/ it seems to work fine for me[/QUOTE] It worked! Thank you very much! There's only one more problem. The sound I have set does not play. How can I fix this?
Emit the sound from the player, not from the weapon.
I'd recommend you not even starting with this. Creating a decent weapon script is moderately difficult. Unless you're just doing it for shits and giggles.
Sorry, you need to Log In to post a reply to this thread.