• Help with this line?
    10 replies, posted
I am trying to edit a simple lua script. I don't know what that code is for what the player is currently holding. I took a wild guess and came up with this [code] if ( [b][i]player.GetCurrentWeapon[/b][/i](self.Owner) ) then [/code] [highlight](User was banned for this post ("Wrong section" - mahalis))[/highlight]
Well, to start off there is no such function. It is called [b][url=http://wiki.garrysmod.com/?title=Player.GetActiveWeapon]Player.GetActiveWeapon [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] and since Lua is an object oriented language, functions work on objects like this: [lua]if ( self.Owner:GetActiveWeapon() ) then[/lua] This is essentially the same as this: [lua]if ( _R.Player.GetActiveWeapon( self.Owner ) ) then[/lua] You can read how this works [url=http://wiki.garrysmod.com/?title=Metatable][b]here[/b][/url].
Alright, thanks alot man! [editline]04:52PM[/editline] Well My attempt at scripting failed. Help? I wanted to pull the stunstick from HL2RP and make it work for DarkRP. Here it is, help me please! [code] if SERVER then AddCSLuaFile("shared.lua") end if (CLIENT) then SWEP.Slot = 0; SWEP.SlotPos = 5; SWEP.DrawAmmo = false; SWEP.PrintName = "Stunstick"; SWEP.DrawCrosshair = true; end SWEP.Instructions = "Primary Fire: Stun.\nSecondary Fire: Push"; SWEP.Purpose = "Stunning disobedient characters, pushing them away and knocking on doors."; SWEP.Contact = ""; SWEP.Author = "kuropixel"; SWEP.WorldModel = "models/weapons/w_stunbaton.mdl"; SWEP.ViewModel = "models/weapons/v_stunstick.mdl"; SWEP.HoldType = "melee"; SWEP.AdminSpawnable = false; SWEP.Spawnable = false; SWEP.Primary.DefaultClip = 0; SWEP.Primary.Automatic = false; SWEP.Primary.ClipSize = -1; SWEP.Primary.Damage = 10; SWEP.Primary.Delay = 1; SWEP.Primary.Ammo = ""; SWEP.Secondary.NeverRaised = true; SWEP.Secondary.DefaultClip = 0; SWEP.Secondary.Automatic = false; SWEP.Secondary.ClipSize = -1; SWEP.Secondary.Delay = 1; SWEP.Secondary.Ammo = ""; SWEP.NoIronSightFovChange = true; SWEP.NoIronSightAttack = true; SWEP.IronSightPos = Vector(0, 0, 0); SWEP.IronSightAng = Vector(0, 0, 0); if (CLIENT) then SWEP.FirstPersonGlowSprite = Material("sprites/light_glow02_add_noz"); SWEP.ThirdPersonGlowSprite = Material("sprites/light_glow02_add"); end; -- Called when the SWEP is deployed. function SWEP:Deploy() self:SendWeaponAnim(ACT_VM_DRAW); end; -- Called when the SWEP is initialized. function SWEP:Initialize() self:SetWeaponHoldType(self.HoldType); end; -- A function to do the SWEP's hit effects. function SWEP:DoHitEffects() local trace = self.Owner:GetEyeTraceNoCursor(); if ( ( (trace.Hit or trace.HitWorld) and self.Owner:GetPos():Distance(trace.HitPos) <= 96 ) ) then self:EmitSound("weapons/stunstick/spark"..math.random(1, 2)..".wav"); if ( IsValid(trace.Entity) and ( trace.Entity:IsPlayer() or trace.Entity:IsNPC() ) ) then self:SendWeaponAnim(ACT_VM_MISSCENTER); self:EmitSound("weapons/stunstick/stunstick_fleshhit"..math.random(1, 2)..".wav"); elseif ( IsValid(trace.Entity) and nexus.entity.GetPlayer(trace.Entity) ) then self:SendWeaponAnim(ACT_VM_MISSCENTER); self:EmitSound("weapons/stunstick/stunstick_fleshhit"..math.random(1, 2)..".wav"); else self:SendWeaponAnim(ACT_VM_MISSCENTER); self:EmitSound("weapons/stunstick/stunstick_impact"..math.random(1, 2)..".wav"); end; local effectData = EffectData(); effectData:SetStart(trace.HitPos); effectData:SetOrigin(trace.HitPos); effectData:SetNormal(trace.HitNormal); if ( IsValid(trace.Entity) ) then effectData:SetEntity(trace.Entity); end; util.Effect("StunstickImpact", effectData, true, true); else self:SendWeaponAnim(ACT_VM_MISSCENTER); self:EmitSound("weapons/stunstick/stunstick_swing"..math.random(1, 2)..".wav"); end; end; -- Called when the world model is drawn. function SWEP:DrawWorldModel() self:DrawModel(); if ( self.Owner:GetActiveWeapon() ) then local attachment = self:GetAttachment(1); local curTime = CurTime(); local scale = math.abs(math.sin(curTime) * 4); local alpha = math.abs(math.sin(curTime) / 4); self.ThirdPersonGlowSprite:SetMaterialFloat("$alpha", 0.7 + alpha); if (attachment and attachment.Pos) then cam.Start3D( EyePos(), EyeAngles() ); render.SetMaterial(self.ThirdPersonGlowSprite); render.DrawSprite( attachment.Pos, 8 + scale, 8 + scale, Color(255, 255, 255, 255 ) ); cam.End3D(); end; end; end; -- Called when the view model is drawn. function SWEP:ViewModelDrawn() if ( self.Owner:GetActiveWeapon() ) then local viewModel = g_LocalPlayer:GetViewModel(); if ( IsValid(viewModel) ) then local attachment = viewModel:GetAttachment( viewModel:LookupAttachment("sparkrear") ); local curTime = CurTime(); local scale = math.abs(math.sin(curTime) * 4); local alpha = math.abs(math.sin(curTime) / 4); -- local i; self.FirstPersonGlowSprite:SetMaterialFloat("$alpha", 0.7 + alpha); self.ThirdPersonGlowSprite:SetMaterialFloat("$alpha", 0.5 + alpha); if (attachment and attachment.Pos) then cam.Start3D( EyePos(), EyeAngles() ); render.SetMaterial(self.ThirdPersonGlowSprite); render.DrawSprite( attachment.Pos, 8 + scale, 8 + scale, Color(255, 255, 255, 255 ) ); self.FirstPersonGlowSprite:SetMaterialFloat("$alpha", 0.5 + alpha); for i = 1, 9 do local attachment = viewModel:GetAttachment( viewModel:LookupAttachment("spark"..i.."a") ); if (attachment.Pos) then if (i == 1 or i == 2 or i == 9) then render.SetMaterial(self.ThirdPersonGlowSprite); else render.SetMaterial(self.FirstPersonGlowSprite); end; render.DrawSprite( attachment.Pos, 1, 1, Color(255, 255, 255, 255) ); end; end; for i = 1, 9 do local attachment = viewModel:GetAttachment( viewModel:LookupAttachment("spark"..i.."b") ); if (attachment.Pos) then if (i == 1 or i == 2 or i == 9) then render.SetMaterial(self.ThirdPersonGlowSprite); else render.SetMaterial(self.FirstPersonGlowSprite); end; render.DrawSprite( attachment.Pos, 1, 1, Color(255, 255, 255, 255) ); end; end; cam.End3D(); end; end; end; end; end; -- A function to do the SWEP's animations. function SWEP:DoAnimations(idle) if (!idle) then self.Owner:SetAnimation(PLAYER_ATTACK1); end; end; -- Called when the player attempts to primary fire. function SWEP:PrimaryAttack() self:SetNextPrimaryFire(CurTime() + self.Primary.Delay); self:SetNextSecondaryFire(CurTime() + self.Primary.Delay); self:DoAnimations(); self:DoHitEffects(); if (SERVER) then if (self.Owner.LagCompensation) then self.Owner:LagCompensation(true); end; local trace = self.Owner:GetEyeTraceNoCursor(); local bounds = Vector(0, 0, 0); local startPosition = self.Owner:GetShootPos(); local finishPosition = startPosition + (self.Owner:GetAimVector() * 96); if (self.Owner:GetShootPos():Distance(trace.HitPos) <= 96) then if ( IsValid(trace.Entity) ) then local player = nexus.entity.GetPlayer(trace.Entity); local strength = nexus.attributes.Fraction(self.Owner, ATB_STRENGTH, 3, 1.5); if ( trace.Entity:IsPlayer() ) then local normal = ( trace.Entity:GetPos() - self.Owner:GetPos() ):Normalize(); local push = 128 * normal; trace.Entity:SetVelocity(push); if (trace.Entity:Health() > 10) then trace.Entity:TakeDamageInfo( NEXUS:FakeDamageInfo(1 + strength, self, self.Owner, trace.HitPos, DMG_CLUB, 2) ); end; nexus.mount.Call("PlayerStunEntity", self.Owner, trace.Entity); elseif ( IsValid( trace.Entity:GetPhysicsObject() ) ) then trace.Entity:GetPhysicsObject():ApplyForceOffset(self.Owner:GetAimVector() * 256, trace.HitPos); if (!player or player:Health() > 10) then if (!player) then trace.Entity:TakeDamageInfo( NEXUS:FakeDamageInfo(5 + (strength * 2), self, self.Owner, trace.HitPos, DMG_CLUB, 2) ); else trace.Entity:TakeDamageInfo( NEXUS:FakeDamageInfo(1 + strength, self, self.Owner, trace.HitPos, DMG_CLUB, 2) ); end; end; nexus.mount.Call("PlayerStunEntity", self.Owner, trace.Entity); end; end; end; if (self.Owner.LagCompensation) then self.Owner:LagCompensation(false); end; end; end; -- Called when the player attempts to secondary fire. function SWEP:SecondaryAttack() if (SERVER) then if (self.Owner.LagCompensation) then self.Owner:LagCompensation(true); end; local trace = self.Owner:GetEyeTraceNoCursor(); if (self.Owner:GetShootPos():Distance(trace.HitPos) <= 96) then if ( IsValid(trace.Entity) ) then if ( self.Owner:GetShootPos():Distance(trace.HitPos) <= 64 and nexus.entity.IsDoor(trace.Entity) ) then self:SetNextPrimaryFire(CurTime() + 0.25); self:SetNextSecondaryFire(CurTime() + 0.25); if (
[QUOTE=Overv;24038432]and since Lua is an object oriented language[/QUOTE] [i]Technically,[/i] lua is a functional programming language, not object oriented. Temex, you need to tell us how it's not working or what errors you're getting so we can help.
Sure Well, I try to spawn it through console 'give tmx_stunstick' and it says that it doesn't exist. [editline]05:02PM[/editline] Null entity or something...
There should be an error somewhere with your file name, copy the whole line.
Nope there isn't. [editline]05:13PM[/editline] All I want is so that for someone to look through the whole script, find anything that wouldn't work unless its with HL2RP Gamemode, edit it so it is a separate swep that can work on it's own, then post it!
[QUOTE=yakahughes;24038685][i]Technically,[/i] lua is a functional programming language, not object oriented.[/QUOTE] True, but Lua is a [url=http://en.wikipedia.org/wiki/Lua_%28programming_language%29]multi-paradigm language[/url] so you can also see it as an [url=http://www.lua.org/pil/28.3.html]object-oriented language with the meta table features[/url].
Can you guys stop arguing about that, and help me ? :S
[QUOTE=Temex;24040000]Can you guys stop arguing about that, and help me ? :S[/QUOTE] [QUOTE=commander204;24038863]There should be an error somewhere with your file name, copy the whole line.[/QUOTE]
Where did you place the SWEP files/folder?
Sorry, you need to Log In to post a reply to this thread.