lua:667: attempt to call method 'DrawViewModel' (a nil value)
9 replies, posted
Whenever I pick a weapon the first time I get this error:
lua:667: attempt to call method 'DrawViewModel' (a nil value)
After that using the same gun nothing happens also when I switch it to another and back to the same.
[Lua]
function SWEP:SetScope(b,player)
if CLIENT or (not player) or player:IsNPC() then return end
local PlaySound = b~= self.Weapon:GetNetworkedBool("Scope", not b) -- Only play zoom sounds when chaning in/out of scope mode
self.CurScopeZoom = 1 -- Just in case...
self.Weapon:SetNetworkedFloat("ScopeZoom",self.ScopeZooms[self.CurScopeZoom])
if b then
player:DrawViewModel(false)
if PlaySound then
self.Weapon:EmitSound(sndZoomIn)
end
else
player:DrawViewModel(true) <---- the line that gives the error
if PlaySound then
self.Weapon:EmitSound(sndZoomOut)
end
end
-- Send the scope state to the client, so it can adjust the player's fov/HUD accordingly
self.Weapon:SetNetworkedBool("Scope", b)
end
[/Lua]
If anyone knows what is wrong here, please leave a reply
Thanks.
You can only draw things on the client. Because of the [i]if CLIENT then return end[/i] then that line won't run on the client.
what you mean to say, that Drawviewmodel only exist on server side?
Can you tell me what should be fixed on this script?
No he said that Drawviewmodel only works clientside remove the if client check or change to if not CLIENT
[Code]
function SWEP:SetScope(b,player)
if (not player) or player:IsNPC() then return end
local PlaySound = b~= self.Weapon:GetNetworkedBool("Scope", not b) -- Only play zoom sounds when chaning in/out of scope mode
self.CurScopeZoom = 1 -- Just in case...
self.Weapon:SetNetworkedFloat("ScopeZoom",self.ScopeZooms[self.CurScopeZoom])
if b then
player:DrawViewModel(false)
if PlaySound then
self.Weapon:EmitSound(sndZoomIn)
end
else
player:DrawViewModel(true) <---- the line that gives the error
if PlaySound then
self.Weapon:EmitSound(sndZoomOut)
end
end
-- Send the scope state to the client, so it can adjust the player's fov/HUD accordingly
self.Weapon:SetNetworkedBool("Scope", b)
end
[/Code]
edit: I've tried your fix but instead the entire base is broken now, (gun ends up frozen)
console is spamming this error:
[ERROR] addons/testsweps/lua/weapons/my_base/cl_init.lua:141: attempt to index a nil value
1. unknown - addons/testsweps/lua/weapons/my_base/cl_init.lua:141
[QUOTE=TigerMeet2142;45396950]edit: I've tried your fix but instead the entire base is broken now, (gun ends up frozen)
console is spamming this error:
[ERROR] addons/testsweps/lua/weapons/my_base/cl_init.lua:141: attempt to index a nil value
1. unknown - addons/testsweps/lua/weapons/my_base/cl_init.lua:141[/QUOTE]
Mind sending us a link to the code?
[Lua]
SWEP.FireModes.Auto.InitFunction = function(self)
self.Primary.Automatic = true
self.Primary.Delay = 60/self.AutoRPM
if CLIENT then
self.FireModeDrawTable.x = 0.037*surface.ScreenWidth()
self.FireModeDrawTable.y = 0.912*surface.ScreenHeight() <--- line 141
end
end
[/Lua]
[editline]15th July 2014[/editline]
If you need the entire code
[QUOTE=TigerMeet2142;45397164][Lua]
SWEP.FireModes.Auto.InitFunction = function(self)
self.Primary.Automatic = true
self.Primary.Delay = 60/self.AutoRPM
if CLIENT then
self.FireModeDrawTable.x = 0.037*surface.ScreenWidth()
self.FireModeDrawTable.y = 0.912*surface.ScreenHeight() <--- line 141
end
end
[/Lua]
[editline]15th July 2014[/editline]
If you need the entire code[/QUOTE]
Are you sure that is [i]cl_init.lua[/i] ? I only ask because of the [i]if CLIENT then[/i] statement.
My bad this was shared, I already felt weird about
[Lua]
function SWEP:DrawHUD()
if self.UseScope then
local bScope = self.Weapon:GetNetworkedBool("Scope")
if bScope ~= self.bLastScope then -- Are we turning the scope off or on
self.bLastScope = bScope
self.fScopeTime = CurTime()
elseif bScope then
local fScopeZoom = self.Weapon:GetNetworkedFloat("ScopeZoom")
if fScopeZoom ~= self.fLastScopeZoom then -- Are we changing the scope zoom level
self.fLastScopeZoom = fScopeZoom
self.fScopeTime = CurTime()
end
end
local fScopeTime = self.fScopeTime or 0
if fScopeTime > CurTime() - SCOPEFADE_TIME then
local Mul = 1.0 -- This scales the alpha
Mul = 1 - math.Clamp((CurTime() - fScopeTime)/SCOPEFADE_TIME, 0, 1)
surface.SetDrawColor(0, 0, 0, 255*Mul) -- Draw a black rect over everything and scale the alpha for a neat fadein effect
surface.DrawRect(0,0,iScreenWidth,iScreenHeight)
end
if bScope then
-- Draw the crosshair
surface.SetDrawColor(0, 0, 0, 150)
surface.DrawLine(self.CrossHairTable.x11,self.CrossHairTable.y11,self.CrossHairTable.x12,self.CrossHairTable.y12)
surface.DrawLine(self.CrossHairTable.x21,self.CrossHairTable.y21,self.CrossHairTable.x22,self.CrossHairTable.y22)
-- Draw the cool parabolic sights
if self.DrawParabolicSights then
surface.SetDrawColor(0, 0, 0, 150)
surface.SetTexture(surface.GetTextureID("rg/rg_parascope"))
surface.DrawTexturedRect(self.ParaScopeTable.x,self.ParaScopeTable.y,self.ParaScopeTable.w,self.ParaScopeTable.h)
end
-- Adds a green filter (used with OICW)
if self.DrawCameraSight then
surface.SetDrawColor(21, 168, 0, 100)
surface.SetTexture(surface.GetTextureID("rg/rg_camerasight"))
surface.DrawTexturedRect(self.ParaScopeTable.x,self.ParaScopeTable.y,self.ParaScopeTable.w,self.ParaScopeTable.h)
end
-- Adds a red crosshair (used with OICW/ P90)
if self.DrawSMODOICWSight then
surface.SetDrawColor(0, 0, 0, 100)
surface.SetTexture(surface.GetTextureID("rg/rg_oicwsight"))
surface.DrawTexturedRect(self.ParaScopeTable.x,self.ParaScopeTable.y,self.ParaScopeTable.w,self.ParaScopeTable.h)
end
-- Draw the lens
surface.SetDrawColor(20,20,20,120)
surface.SetTexture(surface.GetTextureID("overlays/scope_lens"))
surface.DrawTexturedRect(self.LensTable.x,self.LensTable.y,self.LensTable.w,self.LensTable.h)
-- Draw the scope
surface.SetDrawColor(0, 0, 0, 255)
surface.SetTexture(surface.GetTextureID("gui/sniper_corner"))
surface.DrawTexturedRectRotated(self.ScopeTable.x1,self.ScopeTable.y1,self.ScopeTable.l,self.ScopeTable.l,270)
surface.DrawTexturedRectRotated(self.ScopeTable.x2,self.ScopeTable.y2,self.ScopeTable.l,self.ScopeTable.l,180)
surface.DrawTexturedRectRotated(self.ScopeTable.x3,self.ScopeTable.y3,self.ScopeTable.l,self.ScopeTable.l,90)
surface.DrawTexturedRectRotated(self.ScopeTable.x4,self.ScopeTable.y4,self.ScopeTable.l,self.ScopeTable.l,0)
-- Fill in everything else
surface.SetDrawColor(0,0,0,255)
surface.DrawRect(self.QuadTable.x1,self.QuadTable.y1,self.QuadTable.w1,self.QuadTable.h1)
surface.DrawRect(self.QuadTable.x2,self.QuadTable.y2,self.QuadTable.w2,self.QuadTable.h2)
surface.DrawRect(self.QuadTable.x3,self.QuadTable.y3,self.QuadTable.w3,self.QuadTable.h3)
surface.DrawRect(self.QuadTable.x4,self.QuadTable.y4,self.QuadTable.w4,self.QuadTable.h4)
end
end
if not self.DrawFireModes then return end
local FireMode = self.Weapon:GetNetworkedInt("FireMode",1)
self.FireModes[self.AvailableFireModes[FireMode]].HUDDrawFunction(self) -- line 141 <----
end
[/Lua]
I don't know how your code works, or what it's intended purpose it. So, just add this between l.140 and l.141. This will prevent the error but not fix any problems your code will of already had.
[code]
if not self.AvailableFireModes[FireMode] then return end
if not self.FireModes[self.AvailableFireModes[FireMode]] then return end
if not self.FireModes[self.AvailableFireModes[FireMode]].HUDDrawFunction then return end
[/code]
My best guess (purely a guess.) would be that not all FireModes have DrawFunctions, or the value in the AvailableFireModes table (with key FireMode) does not return a valid key for FireModes.
Use the condition statements above to figure out what the true problem is.
Sorry, you need to Log In to post a reply to this thread.