• Cannot work this error out :(
    9 replies, posted
I Have spent ages trying to solve this error, If someopne could help me that would be great, I Will post my code. Thanks [CODE] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( 'shared.lua' ) local SOUND_ON = "items/ammo_pickup.wav" local SOUND_OFF = "" local SOUND_DEAD = "" local BatteryLevel = 100 local dischargeRate = 0.01 local isOn = false function ENT:Initialize() self:SetUseType( SIMPLE_USE ) self:SetModel( "models/items/car_battery01.mdl" ) self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType( MOVETYPE_VPHYSICS ) self:SetSolid( SOLID_VPHYSICS ) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end function ENT:Use( ply) if(BatteryLevel>0) then if(isOn==true) then isOn=false sound.Play( SOUND_ON, self:GetPos() ) else isOn=true sound.Play( SOUND_OFF, self:GetPos() ) end end end function ENT:Think() local attachments = findropes(self) if(attachments!=nil) then for k, v in pairs(attachments) do if (v!=nil) then print(v:GetPos()) end end end end function isWired() BatteryLevel = BatteryLevel - (dischargeRate*FrameTime()) end function findropes(ent) local t = constraint.FindConstraints(ent,"Rope") local tt = {} for k, v in pairs(t) do if v.Entity != nil then table.insert(tt,v.Entity) end end return tt end[/CODE] ERROR : [ERROR] addons/------------/lua/entities/adrug_battery/init.lua:69: attemp call method 'GetPos' (a nil value) 1. unknown - addons/------------/lua/entities/adrug_battery/init.lua:69
Line 69 appears to be " end", 65 seems to be [code]print( v:GetPos( ) ) [/code] Alright, don't use ( v != nil ) if you're trying to see if an Entity ( constraints are entities ) is valid and set... Additionally, if you're using unique data-types which may or may not have GetPos, you can do a check to see if it exists using an if, or ternary... local _pos = ( IsValid( v ) && v.GetPos ) && v:GetPos( ) || vector_origin; -- This says if v is valid and a reference for GetPos exists set _pos to v:GetPos( ), otherwise set _pos to vector_origin. If you want to inverse the logic to remove 1 indent.. remove the if statement end and replace that v!=nil line with: [code]if ( !IsValid( v ) ) then continue; end [/code] OR replace the current line you have and Use: [code]if ( IsValid( v ) ) then [/code] Or, with the v.GetPos check: OR replace the current line you have and Use: [code]if ( IsValid( v ) && v.GetPos ) then [/code] EDIT: Also... Change all [code]var:IsValid( ) [/code] to [code]IsValid( var ) [/code] because if var is not set or nil, then you'll get : Error, attempt to index nil value IsValid... Line 25: [code]if (phys:IsValid()) then [/code] to [code]if ( IsValid( phys ) ) then [/code] Also, there's no need to use isOn == true or isOn == false for varables where you use true/false... use if ( isOn ) then ... or if ( !isOn ) then... if ( isOn ) then will return true for all non-nil and non-false values, while !isOn will return true for nil or false values. 37 change: [code]if(isOn==true) then [/code] to [code]if( isOn ) then [/code] Additionally, you can change that whole segment of code using a toggle... Depending how you code, replace ! with not or leave: [code] if(isOn==true) then isOn=false sound.Play( SOUND_ON, self:GetPos() ) else isOn=true sound.Play( SOUND_OFF, self:GetPos() ) end [/code] to [code] if( isOn ) then sound.Play( SOUND_ON, self:GetPos() ) else sound.Play( SOUND_OFF, self:GetPos() ) end // This toggles the value from nil/false to true to false to true... isOn = !isOn; [/code] Line 67 or so read up.. Line 88 or so, read up.. [code]if v.Entity != nil then [/code] to [code]if IsValid( v.Entity ) then [/code] Hopefully this helps.
[QUOTE=] Hopefully this helps.[/QUOTE] Thanks for the help but no, Now I get this error [ERROR] addons/-------------/lua/entities/adrug_battery/init.lua:92: attempt to call global 'isValid' (a nil value) 1. findropes - addons/---------------/lua/entities/adrug_battery/init.lua:92 2. unknown - addons/------------/lua/entities/adrug_battery/init.lua:61
IsValid NOT isValid
[QUOTE=freshmintyy;46875292]IsValid NOT isValid[/QUOTE] Now I jsut feel like a complete dick.... xD [editline]7th January 2015[/editline] [QUOTE=darkmist;46875311]Now I jsut feel like a complete dick.... xD[/QUOTE] Never mind, Still come back to the same error at the start [CODE][ERROR] addons/advanced drugs/lua/entities/adrug_battery/init.lua:73: attempt to call method 'GetPos' (a nil value) 1. unknown - addons/advanced drugs/lua/entities/adrug_battery/init.lua:73 [/CODE] [editline]7th January 2015[/editline] I Think the problems lies here somewhere, Would this code return a table of valid entities? [CODE]function findropes(ent) local t = constraint.FindConstraints(ent,"Rope") local tt = {} for k, v in pairs(t) do if IsValid(v) then table.insert(tt,v) end end return tt end[/CODE] This was suggested by someone on the forum the use this code. So I dont have the documentation for it
try changing self:GetPos() to ply:GetPos(). Test that out in the ENT:Use function
[QUOTE=freshmintyy;46875970]try changing self:GetPos() to ply:GetPos(). Test that out in the ENT:Use function[/QUOTE] wHERE DO i USE self:GetPos() ?
[code]function ENT:Use( ply) if(BatteryLevel>0) then if(isOn==true) then isOn=false sound.Play( SOUND_ON, self:GetPos() ) else isOn=true sound.Play( SOUND_OFF, self:GetPos() ) end end end[/code]
[QUOTE=freshmintyy;46876022][code]function ENT:Use( ply) if(BatteryLevel>0) then if(isOn==true) then isOn=false sound.Play( SOUND_ON, self:GetPos() ) else isOn=true sound.Play( SOUND_OFF, self:GetPos() ) end end end[/code][/QUOTE] The error is not there. And I used self referring to the entities position, Not the play so ply would not work. [ERROR] addons/advanced drugs/lua/entities/adrug_battery/init.lua:73: attempt to call method 'GetPos' (a nil value) 1. unknown - addons/advanced drugs/lua/entities/adrug_battery/init.lua:73 Thats the errror From here local attachments = findropes(self) if(attachments!=nil) then for k, v in pairs(attachments) do if (v!=nil) then print(v:GetPos()) end end end
[QUOTE=darkmist;46875311] Never mind, Still come back to the same error at the start [CODE][ERROR] addons/advanced drugs/lua/entities/adrug_battery/init.lua:73: attempt to call method 'GetPos' (a nil value) 1. unknown - addons/advanced drugs/lua/entities/adrug_battery/init.lua:73 [/CODE][/QUOTE] Read my post, it covers a lot... [QUOTE=Acecool;46874998]Line 69 appears to be " end", 65 seems to be [code]print( v:GetPos( ) ) [/code] OR replace the current line you have and Use: [code]if ( IsValid( v ) ) then [/code] Or, with the v.GetPos check: OR replace the current line you have and Use: [code]if ( IsValid( v ) && v.GetPos ) then [/code] Hopefully this helps.[/QUOTE] [editline]8th January 2015[/editline] -what I typed up but didn't click post on, whoops- Add this check: if ( IsValid( v ) && v.GetPos ) then Also, add an ELSE. Print out type( v ), v.MetaName and other info. Part of debugging is printing out data. If you want to make printing things easier, I added almost all meta-tables to concat and print and hased
Sorry, you need to Log In to post a reply to this thread.