Good Evening!
The error: attempt to perform arithmetic on field 'ts' (a nil value)
I guess I use the variable that was defined later in the code. Where or why?
The Code:
include("shared.lua")
surface.CreateFont("Title",{font = "Arial",extended = false,size = 80, weight = 500})
function ENT:Initialize()
self.ts = 255
end
function ENT:Trans()
timer.Create( "Hide", 0.1, 255, function() self.ts = self.ts - 1 end )
timer.Create( "Show", 0.1, 255, function() self.ts = self.ts + 1 end )
end
local work_icon = Material( "icons/work.png" )
function ENT:Draw()
self:DrawModel()
local alpha = 255
local viewdist = 1000
local max = viewdist
local min = viewdist * 0.75
local dist = LocalPlayer():EyePos():Distance(self:GetPos())
local oang = self:GetAngles()
local ang1 = self:GetAngles()
local pos1 = self:GetPos()
ang1:RotateAroundAxis(oang:Up(), -270)
--ang1:RotateAroundAxis(oang:Right(), 0)
pos0 = pos1 + oang:Forward() * -20 + oang:Up() * 2 + oang:Right() * 40
if dist > min and dist < max then
alpha = 255
elseif dist > max then
alpha = 0
end
cam.Start3D2D(pos0 + oang:Forward() * 20 + oang:Right() * -40,ang1,0.1)
surface.SetMaterial(work_icon)
surface.SetDrawColor( 255, 255, 255,self.ts)
surface.DrawTexturedRectRotated( 0, 0,500,500, CurTime() % 360 * -120)
cam.End3D2D()
net.Receive("DoTrans",function()
print("turkey")
self:Trans()
end)
end
My suggestion would just to be make a local variable in the file, instead of your method. It'd be better to have the full error, then we could understand where the issue exactly starts, but basically "ts" isn't defined.
local ts
function ENT:Initialize()
ts = 255
end
Try that out, just remember to change all the "self.ts" to just "ts"
A local variable isn't an option here, otherwise other instances of the entity would overwrite it. It would be probably better to just do ENT.ts = 255 instead. However, the full error would be appreciated.
Well first: you need to move that net.Receive out of the Draw hook. You'll have to send the entity with net.WriteEntity and use net.ReadEntity instead of self.
Also your timers will have problems if more than one entity is going to do whatever you want, because the timer names are not unique to the entity.
You also need to check to make sure self is valid inside of the timers in case it gets removed.
I'd recommend skipping the timers and using the entity's think hook and some variables. Something like:
function ENT:Think()
if self.hide then
if self.trans <= 0 then -- stop decreasing it at (or under) 0
self.hide = false
return
end
self.trans = self.trans - 1
end
if self.show then
if self.trans >= 255 then -- stop increasing it at 255
self.show = false
return
end
self.trans = self.trans + 1
end
self:SetNextClientThink( CurTime() + 0.1 ) -- Make it so Think won't call for another 0.1 seconds
end
and then make the net receive set hide/trans respectively (why do you do both at the same time it'd cancel each-other out?)
As for the nil, I'd guess that the problem is caused by doing net.Receive in the draw hook, see the second warning at net.Receive.
A performance tip is to replace Vector/Distance with Vector/DistToSqr and just square your distance (so use 1000*1000 instead of 1000)
I know it's more than you asked for but
Thank you too much! I rewrote it a little bit.
function ENT:Think()
if self.hide then
if self.trans <= 0 then -- stop decreasing it at (or under) 0
self.show = true
self.hide = false
print("revers " .. self.trans)
end
self.trans = self.trans - 1
end
if self.show then
if self.trans >= 255 then -- stop increasing it at 255
print("topf")
self.hide = true
self.show = false
end
self.trans = self.trans + 1
end
self:SetNextClientThink( CurTime() + 0.01 ) -- Make it so Think won't call for another 0.1 seconds
end
Sorry, you need to Log In to post a reply to this thread.