• SWEP ERRORS HELP!
    9 replies, posted
it works very well, but it is able to shoot without ammo... It should takes ammo and can't shot if has no ammo , but it is not. And appears error [weapons\weapon_ttt_headcrablauncher\shared.lua:70] attempt to index global 'LocalPlayer' (a function value) here is the code [lua] -- ***** -- Version with English.lua tags (Requires editing English.lua, see attached TXT) -- Kudos to myself (Stu Alpha) for getting the basic weapon working ingame -- My name is stu alpha and I can't add names or descriptions -stone -- Plus I made it better -- ***** if SERVER then AddCSLuaFile( "shared.lua" ) end SWEP.HoldType = "rpg" if CLIENT then SWEP.PrintName = "Headcrab Launcher" SWEP.Slot = 6 SWEP.EquipMenuData = { type = "EXTREME WEAPON", -- Insert AWP Description here desc = "Your.....a....nothing" }; -- Insert AWP Icon here SWEP.Icon = "VGUI/ttt/icon_headcrabs" end if SERVER then resource.AddFile("materials/VGUI/ttt/icon_headcrabs.vmt") end SWEP.Base = "weapon_tttbase" SWEP.Spawnable = false SWEP.AdminSpawnable = true SWEP.DrawCrosshair = false SWEP.NoSights = true SWEP.Kind = WEAPON_EQUIP SWEP.CanBuy = {ROLE_TRAITOR} SWEP.LimitedStock = true SWEP.WeaponID = RPG 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.ViewModel = "models/weapons/v_RPG.mdl" SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl" local ShootSoundFire = Sound( "Airboat.FireGunHeavy" ) local ShootSoundFail = Sound( "WallHealth.Deny" ) local YawIncrement = 20 local PitchIncrement = 10 if CLIENT then language.Add ("Undone_CrabLaunch", "Undone Headcrab Canister.") end function SWEP:PrimaryAttack(bSecondary) if LocalPlayer:GetAmmoCaount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) <= 0 then return end local tr = self.Owner:GetEyeTrace() self:ShootEffects(self) self:TakePrimaryAmmo(1) if (SERVER) then local aBaseAngle = tr.HitNormal:Angle() local aBasePos = tr.HitPos local bScanning = true local iPitch = 10 local iYaw = -180 local iLoopLimit = 0 local iProcessedTotal = 0 local tValidHits = {} while (bScanning == true && iLoopLimit < 500) do iYaw = iYaw + YawIncrement iProcessedTotal = iProcessedTotal + 1 if (iYaw >= 180) then iYaw = -180 iPitch = iPitch - PitchIncrement end local tLoop = util.QuickTrace( aBasePos, (aBaseAngle+Angle(iPitch,iYaw,0)):Forward()*40000 ) if (tLoop.HitSky || bSecondary) then table.insert(tValidHits,tLoop) end if (iPitch <= -80) then bScanning = false end iLoopLimit = iLoopLimit + 1 end local iHits = table.Count(tValidHits) if (iHits > 0) then local iRand = math.random(1,iHits) local tRand = tValidHits[iRand] local ent = ents.Create( "env_headcrabcanister" ) ent:SetPos( aBasePos ) ent:SetAngles( (tRand.HitPos-tRand.StartPos):Angle() ) ent:SetKeyValue( "HeadcrabType", math.random(0,2) ) ent:SetKeyValue( "HeadcrabCount", math.random(2,5) ) ent:SetKeyValue( "FlightSpeed", math.random(2500,6000) ) ent:SetKeyValue( "FlightTime", math.random(2,5) ) ent:SetKeyValue( "Damage", math.random(50,90) ) ent:SetKeyValue( "DamageRadius", math.random(300,512) ) ent:SetKeyValue( "SmokeLifetime", math.random(5,10) ) ent:SetKeyValue( "StartingHeight", 1000 ) local iSpawnFlags = 8192 if (bSecondary) then iSpawnFlags = iSpawnFlags + 4096 end //If Secondary, spawn impacted. ent:SetKeyValue( "spawnflags", iSpawnFlags ) ent:Spawn() ent:Input("FireCanister", self.Owner, self.Owner) undo.Create("CrabLaunch") undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.AddFunction(function(undo) for k, v in pairs(ents.FindByClass("npc_headcrab*"))do if (v:GetOwner() == ent) then v:Remove() end end end) undo.Finish() self:EmitSound( ShootSoundFire ) else self:EmitSound( ShootSoundFail ) end tLoop = nil tValidHits = nil end end function SWEP:ShouldDropOnDie() return false end [/lua]
Line 70: You missed the parentheses after [B]LocalPlayer[/B] and misspelled [B]Count[/B]. [lua] if LocalPlayer:GetAmmoCaount [/lua] Should be: [lua] if LocalPlayer():GetAmmoCount [/lua]
[QUOTE=thefreeman193;31024727]Line 70: You missed the parentheses after [B]LocalPlayer[/B] and misspelled [B]Count[/B]. [lua] if LocalPlayer:GetAmmoCaount [/lua] Should be: [lua] if LocalPlayer():GetAmmoCount [/lua][/QUOTE] You forgot (), should be: [lua] if LocalPlayer():GetAmmoCount(parahere) then [/lua]
[QUOTE=_NewBee;31025670]You forgot (), should be: [lua] if LocalPlayer():GetAmmoCount(parahere) then [/lua][/QUOTE] [lua] function SWEP:PrimaryAttack(bSecondary) if LocalPlayer():GetAmmoCount((LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) <= 0) then return end local tr = self.Owner:GetEyeTrace() self:ShootEffects(self) self:TakePrimaryAmmo(1) if (SERVER) then local aBaseAngle = tr.HitNormal:Angle() local aBasePos = tr.HitPos local bScanning = true local iPitch = 10 local iYaw = -180 local iLoopLimit = 0 local iProcessedTotal = 0 local tValidHits = {} [/lua] is it right?
Yeah, but cyw960517 could probably work that out :v: Anyway, i've just noticed another problem. Are you trying to get the ammo in the weapon or just the ammo being carried? You should really be using: [lua] if LocalPlayer():GetActiveWeapon():Clip1() <= 0 then self:Reload() --If they run out of ammo then try to fire, reload automatically. Remove this if you don't want it. return false end [/lua]
[QUOTE=thefreeman193;31026580]Yeah, but cyw960517 could probably work that out :v: Anyway, i've just noticed another problem. Are you trying to get the ammo in the weapon or just the ammo being carried? You should really be using: [lua] if LocalPlayer():GetActiveWeapon():Clip1() <= 0 then self:Reload() --If they run out of ammo then try to fire, reload automatically. Remove this if you don't want it. return false end [/lua][/QUOTE] [lua] function SWEP:PrimaryAttack(bSecondary) if LocalPlayer():GetAmmoCount((LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) <= 0) then return end local tr = self.Owner:GetEyeTrace() self:ShootEffects(self) self:TakePrimaryAmmo(1) if (SERVER) then local aBaseAngle = tr.HitNormal:Angle() local aBasePos = tr.HitPos local bScanning = true local iPitch = 10 local iYaw = -180 local iLoopLimit = 0 local iProcessedTotal = 0 local tValidHits = {} [/lua] the code was not worked. And i have no idea what is that mean (trying to get the ammo in the weapon or just the ammo being carried) I just want to make this be able to shoot only one bullet. And It was not worked too [lua] function SWEP:PrimaryAttack(bSecondary) if LocalPlayer():GetActiveWeapon():Clip1() <= 0 then self:Reload() --If they run out of ammo then try to fire, reload automatically. Remove this if you don't want it. return false end local tr = self.Owner:GetEyeTrace() self:ShootEffects(self) self:TakePrimaryAmmo(1) if (SERVER) then local aBaseAngle = tr.HitNormal:Angle() local aBasePos = tr.HitPos local bScanning = true local iPitch = 10 local iYaw = -180 local iLoopLimit = 0 local iProcessedTotal = 0 local tValidHits = {} [/lua]
weapon:GetAmmoCount() only includes the ammo that the player has (the small number on your HUD). weapon:Clip1() returns the amount of ammuntion in the current clip (the big number on your HUD), and that's what you'll be needing. I'll have a look at the rest of the script... [editline]10th July 2011[/editline] Do you want it so it can only fire it once then it's useless or so you have to reload before you can use it again?
[QUOTE=thefreeman193;31027327]weapon:GetAmmoCount() only includes the ammo that the player has (the small number on your HUD). weapon:Clip1() returns the amount of ammuntion in the current clip (the big number on your HUD), and that's what you'll be needing. I'll have a look at the rest of the script...[/QUOTE] Thanks! i will check your post :D
[QUOTE=thefreeman193;31027327]Do you want it so it can only fire it once then it's useless or so you have to reload before you can use it again?[/QUOTE] He wants it soyou can only shoot it once.
Try this: [lua] -- ***** -- Version with English.lua tags (Requires editing English.lua, see attached TXT) -- Kudos to myself (Stu Alpha) for getting the basic weapon working ingame -- My name is stu alpha and I can't add names or descriptions -stone -- Plus I made it better -- ***** if SERVER then AddCSLuaFile("shared.lua") SWEP.Weight = 5 resource.AddFile("materials/VGUI/ttt/icon_headcrabs.vmt") elseif CLIENT then SWEP.PrintName = "Headcrab Launcher" SWEP.Slot = 5 SWEP.SlotPos = 3 SWEP.Author = "NAME" SWEP.Instructions = "INSTRUCTIONS" SWEP.DrawCrosshair = false SWEP.EquipMenuData = { type = "EXTREME WEAPON", desc = "Your.....a....nothing" } -- Insert AWP Icon here SWEP.Icon = "VGUI/ttt/icon_headcrabs" end SWEP.Base = "weapon_tttbase" SWEP.Spawnable = false SWEP.AdminSpawnable = true SWEP.ViewModel = "models/weapons/v_RPG.mdl" SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl" SWEP.NoSights = true SWEP.Kind = WEAPON_EQUIP SWEP.CanBuy = {ROLE_TRAITOR} SWEP.LimitedStock = true SWEP.WeaponID = RPG 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 ShootSoundFire = Sound("Airboat.FireGunHeavy") local ShootSoundFail = Sound("WallHealth.Deny") local YawIncrement = 20 local PitchIncrement = 10 if CLIENT then language.Add("Undone_CrabLaunch", "Undone Headcrab Canister.") end function SWEP:Initialize() if SERVER then self:SetWeaponHoldType(self.HoldType) end self:SetNWBool("Used", false) end function SWEP:PrimaryAttack(bSecondary) if self:GetNWBool("Used", false) then return false end local tr = self.Owner:GetEyeTrace() local aBaseAngle = tr.HitNormal:Angle() local aBasePos = tr.HitPos local bScanning = true local iPitch = 10 local iYaw = -180 local iLoopLimit = 0 local iProcessedTotal = 0 local tValidHits = {} while (bScanning && iLoopLimit < 500) do iYaw = iYaw + YawIncrement iProcessedTotal = iProcessedTotal + 1 if (iYaw >= 180) then iYaw = -180 iPitch = iPitch - PitchIncrement end local tLoop = util.QuickTrace(aBasePos, (aBaseAngle+Angle(iPitch,iYaw,0)):Forward()*40000) if (tLoop.HitSky || bSecondary) then table.insert(tValidHits,tLoop) end if (iPitch <= -80) then bScanning = false end iLoopLimit = iLoopLimit + 1 end local iHits = table.Count(tValidHits) if (iHits > 0) then self:SetNWBool("Used", true) self:SendWeaponAnim(ACT_VM_PRIMARYATTACK) if SERVER then self.Owner:SetAnimation(PLAYER_ATTACK1) local iRand = math.random(1,iHits) local tRand = tValidHits[iRand] local ent = ents.Create("env_headcrabcanister") ent:SetPos(aBasePos) ent:SetAngles((tRand.HitPos-tRand.StartPos):Angle()) ent:SetKeyValue("HeadcrabType", math.random(0,2)) ent:SetKeyValue("HeadcrabCount", math.random(2,5)) //ent:SetKeyValue("HeadcrabCount", 0) ent:SetKeyValue("FlightSpeed", math.random(2500,6000)) ent:SetKeyValue("FlightTime", math.random(2,5)) ent:SetKeyValue("Damage", math.random(50,90)) ent:SetKeyValue("DamageRadius", math.random(300,512)) ent:SetKeyValue("SmokeLifetime", math.random(5,10)) ent:SetKeyValue("StartingHeight", 1000) local iSpawnFlags = 8192 if (bSecondary) then iSpawnFlags = iSpawnFlags + 4096 end //If Secondary, spawn impacted. ent:SetKeyValue("spawnflags", iSpawnFlags) ent:Spawn() ent:Input("FireCanister", self.Owner, self.Owner) undo.Create("CrabLaunch") undo.AddEntity(ent) undo.SetPlayer(self.Owner) undo.AddFunction(function(undo) for k, v in pairs(ents.FindByClass("npc_headcrab*"))do if (v:GetOwner() == ent) then v:Remove() end end end) undo.Finish() self:EmitSound(ShootSoundFire) end else self:EmitSound(ShootSoundFail) end tLoop = nil tValidHits = nil return true end function SWEP:ShouldDropOnDie() return false end [/lua]
Sorry, you need to Log In to post a reply to this thread.