Lua Errors when going inside skybox

Hello everyone.

My script is creating errors when going inside the skybox and sometimes when going through a doorway I think.

Function used: [LUA] function GM:PreDrawHalos() [/LUA]
Error:
[LUA]
[ERROR] gamemodes/downtownrp/gamemode/modules/essentials/cl_halo.lua:6: Tried to use a NULL entity!

  1. GetPos - [C]:-1
  2. Run - gamemodes/downtownrp/gamemode/modules/essentials/cl_halo.lua:6
    3. fn - lua/includes/modules/halo.lua:165
    4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
    [/LUA]

Code: (Starting at line 6)
[LUA]
if LocalPlayer():GetEyeTrace().Entity:GetClass() == “spawned_weapon” then
local ent = LocalPlayer():GetEyeTrace().Entity
local tab = {}
table.insert( tab, ent )
if LocalPlayer():GetEyeTrace().Entity:GetPos():Distance(LocalPlayer():GetPos()) < 200 then
halo.Add(tab, Color(255, 0, 0), 5, 5, 2, true, false)
end
end
[/LUA]

“LocalPlayer():GetEyeTrace().Entity” isn’t always valid, do a IsValid( LocalPlayer():GetEyeTrace().Entity ) check.

Looks like it is still giving me the same error. :confused:

You gotta show the new code.

Error: (Same)
[LUA]
[ERROR] gamemodes/downtownrp/gamemode/modules/essentials/cl_halo.lua:6: Tried to use a NULL entity!

  1. GetClass - [C]:-1
  2. Run - gamemodes/downtownrp/gamemode/modules/essentials/cl_halo.lua:6
    3. fn - lua/includes/modules/halo.lua:165
    4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
    [/LUA]

New Code: (Starting line 6)
[LUA]
if IsValid( LocalPlayer():GetEyeTrace().Entity:GetClass() == “spawned_weapon” ) then
local ent = LocalPlayer():GetEyeTrace().Entity
local tab = {}
table.insert( tab, ent )
if LocalPlayer():GetEyeTrace().Entity:GetPos():Distance(LocalPlayer():GetPos()) < 200 then
halo.Add(tab, Color(255, 0, 0), 5, 5, 2, true, false)
end
end
[/LUA]

[lua]
local ply = LocalPlayer()
local plyEyeTr = ply:GetEyeTrace()
local plyEyeTrEnt = plyEyeTr.Entity
if IsValid(plyEyeTrEnt) and (plyEyeTrEnt:GetClass() == “spawned_weapon”) then
if plyEyeTrEnt:GetPos():Distance(ply:GetPos()) < 200 then
halo.Add({ent}, Color(255, 0, 0), 5, 5, 2, true, false)
end
end
[/lua]

Oh hold on. I was wrong. For the spawned_weapon it worked. But when I have multiple entities wanting to halo like this:

[LUA]
if IsValid(LocalPlayer():GetEyeTrace().Entity) and LocalPlayer():GetEyeTrace().Entity:GetClass() == “silver_printer” or LocalPlayer():GetEyeTrace().Entity:GetClass() == “golden_printer” or LocalPlayer():GetEyeTrace().Entity:GetClass() == “weed_plant”
or LocalPlayer():GetEyeTrace().Entity:GetClass() == “weed_seed” or LocalPlayer():GetEyeTrace().Entity:GetClass() == “weed” then
local ent = LocalPlayer():GetEyeTrace().Entity
local tab = {}
table.insert( tab, ent )
if LocalPlayer():GetEyeTrace().Entity:GetPos():Distance(LocalPlayer():GetPos()) < 200 then
halo.Add(tab, Color(0, 0, 255), 5, 5, 2, true, false)
end
end
[/LUA]

Then it gives me the error again.

Stop using table.insert, it’s redundant to use it for your purpose.

[lua]
local EntsToHalo = {
silver_printer = true,
golden_printer = true
}

local function DRPHaloDrawing()
local ply = LocalPlayer()
local plyEyeTr = ply:GetEyeTrace()
local plyEyeTrEnt = plyEyeTr.Entity
local canHaloEnt = EntsToHalo[plyEyeTrEnt:GetClass()]
if IsValid(plyEyeTrEnt) and canHaloEnt then
if plyEyeTrEnt:GetPos():Distance(ply:GetPos()) < 200 then
halo.Add({ent}, Color(255, 0, 0), 5, 5, 2, true, false)
end
end
end
[/lua]

Doesn’t work

You’re really not helping yourself: don’t give us nothing and expect us to work miracles. Luckily I can work miracles; you need to hook the function to PreDrawHalos.

[lua]hook.Add( “PreDrawHalos”, “DRPHalos”, DRPHaloDrawing )

I’m very sorry but this all doesn’t work :confused:

  • The halo doesn’t activate.
  • There is no lua error in console.

Here, this should work. If not, you are doing something horribly wrong, and should not code LUA. Ever.

[LUA]-- Add entities to draw in this table, following the syntax
local HaloEnts = {
silver_printer = true,
golden_printer = true,
weed_plant = true,
weed_seed = true,
weed = true
}

– Function to draw a glow effect around certain entities
local function DrawHalos()
local trace = LocalPlayer():GetEyeTrace()
if IsValid(trace.Entity) && HaloEnts[traceEnt:GetClass()] then
if trace.Entity:GetPos():Distance(LocalPlayer():GetPos()) < 200 then
halo.Add(trace.Entity, Color(255, 0, 0), 5, 5, 2, true, false) – Draw a red halo.
end
end
end
hook.Add( “PreDrawHalos”, “DRPDrawHalos”, DrawHalos ) – Add function to PreDrawHalos hook[/LUA]

This also does not work. First it was creating a error about
[LUA] traceEnt [/LUA]
Wich should have been [LUA] LocalPlayer():GetEyeTrace().Entity [/LUA]

But then it black screen because simply the halo function is not being the way it should by you guys.
I will leave it the way it was before this thread as there appears to be no solution for this problem.

Black screen ended up with this error
[LUA]
[ERROR] lua/includes/modules/halo.lua:66: bad argument #1 to ‘pairs’ (table expected, got userdata)

  1. pairs - [C]:-1
  2. Render - lua/includes/modules/halo.lua:66
    3. fn - lua/includes/modules/halo.lua:170
    4. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
    [/LUA]

Maybe it would be a thing to actually test it yourself, before posting it here.

Just change traceEnt to trace.Entity. This code is fixed:
[LUA]-- Add entities to draw in this table, following the syntax
local HaloEnts = {
silver_printer = true,
golden_printer = true,
weed_plant = true,
weed_seed = true,
weed = true
}

– Function to draw a glow effect around certain entities
local function DrawHalos()
local trace = LocalPlayer():GetEyeTrace()
if IsValid(trace.Entity) && HaloEnts[trace.Entity:GetClass()] then
if trace.Entity:GetPos():Distance(LocalPlayer():GetPos()) < 200 then
halo.Add(trace.Entity, Color(255, 0, 0), 5, 5, 2, true, false) – Draw a red halo.
end
end
end
hook.Add( “PreDrawHalos”, “DRPDrawHalos”, DrawHalos ) – Add function to PreDrawHalos hook[/LUA]

As for the other error, that’s your fault. I did not use a single “for” loop in my code. Maybe post the code you want fixed instead of posting errors?

Posted from my mac, i don’t have Garry’s Mod installed on my mac.

facepalm