• Can't understand why I get attempted to index global "self" ( a nil value )
    6 replies, posted
Yeah, i've been trying to figure out what im doing wrong right now, i keep getting "attempted to index global "-" a nil value The script is supposed to let the player slide when he holds down Forward and ducks, but i cant seem to figure out why it gives me the error stated, i tried searching and all that but the solutions are for a very specific scenario and they dont seem to work on my script. my script is a autorun script [CODE]local ply = self.Owner hook.Add( "Tick", "Slide", function() if ply:WaterLevel() <= 1 then if !IsValid( ply ) then return end local pos = self:GetOperatingPoint() ply.SlidingSound = ply.SlidingSound or CreateSound( ply, 'slidemod/floorslide.wav' ) if ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_DUCK ) and ply:IsOnGround() then if ply:GetVelocity():Length() < 150 then if ply.SlidingSound:IsPlaying() then ply.SlidingSound:Stop() end return end ply.SlideVelocity = ply.SlideVelocity or 0 if ply.SlideVelocity == 0 then ply.SlideVelocity = 70 * ( math.Clamp( ply:GetVelocity():Length() / 500, 0, 1 ) ) end local dir = Angle( 0, ply:EyeAngles().y, 0 ):Forward() local tr = util.TraceLine( { start = pos, endpos = pos + dir * 35, filter = ply } ) local tr_up = util.TraceLine( { start = pos - Vector( 0, 0, 40 ), endpos = pos - Vector( 0, 0, 43 ) + dir * 25, filter = ply } ) local tr_down = util.TraceLine( { start = pos - Vector( 0, 0, 40 ), endpos = pos - Vector( 0, 0, 48 ) + dir * 25, filter = ply } ) if !tr_down.Hit then ply.SlideVelocity = math.Approach( ply.SlideVelocity, 100, 0.5 ) else ply.SlideVelocity = math.Approach( ply.SlideVelocity, 0, 0.5 ) end if tr_up.Hit then ply.SlideVelocity = math.Approach( ply.SlideVelocity, 0, 2 ) end if tr.Hit and ply.SlideVelocity > 0 then if IsValid( tr.Entity ) and ( tr.Entity:IsPlayer() or tr.Entity:IsNPC() ) then tr.Entity:TakeDamage( 70 * ( math.Clamp( ply:GetVelocity():Length() / 1000, 0, 1 ) ), self.Owner, self ) end ply:ViewPunch( Angle( ply.SlideVelocity / 5, 0.3, -1 ) ) ply.SlideVelocity = 0 if ply.SlidingSound:IsPlaying() then ply.SlidingSound:Stop() ply:EmitSound( 'slidemod/floorslide_hit_hard.wav' ) end return end ply:ViewPunch( Angle( 0, 0.3, -1 ) ) ply:SetVelocity( dir * ply.SlideVelocity ) if !ply.SlidingSound:IsPlaying() then ply.SlidingSound:Play() end else ply.SlideVelocity = 0 if ply.SlidingSound and ply.SlidingSound:IsPlaying() then ply.SlidingSound:Stop() end end end end )[/CODE]
Post your error please
[QUOTE=Rmk123;48162867]Post your error please[/QUOTE] [ERROR] addons/sliding/lua/autorun/slide.lua:1: attempt to index global 'self' (a nil value) 1. unknown - addons/sliding/lua/autorun/slide.lua:1
You haven't defined ply properly. Self is nil in other words nothing. You'll have to retrieve the player somehow else.
[QUOTE=jellofacey;48162861]Yeah, i've been trying to figure out what im doing wrong right now, i keep getting "attempted to index global "-" a nil value The script is supposed to let the player slide when he holds down Forward and ducks, but i cant seem to figure out why it gives me the error stated, i tried searching and all that but the solutions are for a very specific scenario and they dont seem to work on my script. my script is a autorun script [CODE]local ply = self.Owner hook.Add( "Tick", "Slide", function() if ply:WaterLevel() <= 1 then if !IsValid( ply ) then return end local pos = self:GetOperatingPoint() ply.SlidingSound = ply.SlidingSound or CreateSound( ply, 'slidemod/floorslide.wav' ) if ply:KeyDown( IN_FORWARD ) and ply:KeyDown( IN_DUCK ) and ply:IsOnGround() then if ply:GetVelocity():Length() < 150 then if ply.SlidingSound:IsPlaying() then ply.SlidingSound:Stop() end return end ply.SlideVelocity = ply.SlideVelocity or 0 if ply.SlideVelocity == 0 then ply.SlideVelocity = 70 * ( math.Clamp( ply:GetVelocity():Length() / 500, 0, 1 ) ) end local dir = Angle( 0, ply:EyeAngles().y, 0 ):Forward() local tr = util.TraceLine( { start = pos, endpos = pos + dir * 35, filter = ply } ) local tr_up = util.TraceLine( { start = pos - Vector( 0, 0, 40 ), endpos = pos - Vector( 0, 0, 43 ) + dir * 25, filter = ply } ) local tr_down = util.TraceLine( { start = pos - Vector( 0, 0, 40 ), endpos = pos - Vector( 0, 0, 48 ) + dir * 25, filter = ply } ) if !tr_down.Hit then ply.SlideVelocity = math.Approach( ply.SlideVelocity, 100, 0.5 ) else ply.SlideVelocity = math.Approach( ply.SlideVelocity, 0, 0.5 ) end if tr_up.Hit then ply.SlideVelocity = math.Approach( ply.SlideVelocity, 0, 2 ) end if tr.Hit and ply.SlideVelocity > 0 then if IsValid( tr.Entity ) and ( tr.Entity:IsPlayer() or tr.Entity:IsNPC() ) then tr.Entity:TakeDamage( 70 * ( math.Clamp( ply:GetVelocity():Length() / 1000, 0, 1 ) ), self.Owner, self ) end ply:ViewPunch( Angle( ply.SlideVelocity / 5, 0.3, -1 ) ) ply.SlideVelocity = 0 if ply.SlidingSound:IsPlaying() then ply.SlidingSound:Stop() ply:EmitSound( 'slidemod/floorslide_hit_hard.wav' ) end return end ply:ViewPunch( Angle( 0, 0.3, -1 ) ) ply:SetVelocity( dir * ply.SlideVelocity ) if !ply.SlidingSound:IsPlaying() then ply.SlidingSound:Play() end else ply.SlideVelocity = 0 if ply.SlidingSound and ply.SlidingSound:IsPlaying() then ply.SlidingSound:Stop() end end end end )[/CODE][/QUOTE] you can't use self in an autorun script, self only works inside entities or metafunctions try using a for loop to run that logic on all players
self refers to the table object a function is running on for example, [lua] mytable = {} mytable.penis = 1 function mytable:MorePenis() self.penis = self.penis+1 end [/lua] self is only valid in a function running off of a table object
oh now i get it, ill try what robbo suggested, thanks
Sorry, you need to Log In to post a reply to this thread.