'PrintMessage' (a nil value) - works after lua_reloadents

I have been creating a basic lua stool to return the mass of a physics object when the user left clicks. Here is the code so far:


TOOL.Category = "Danilo's Tools"
TOOL.Name = "Object Mass"
TOOL.Command = nil
TOOL.ConfigName = ""

function TOOL:LeftClick(trace)
if not trace.Entity then return end
local physobj = trace.Entity:GetPhysicsObject()
if physobj then
self.Owner:PrintMessage(HUD_PRINTTALK, physobj:GetMass())
end
end

Currently, upon start up of the game (fresh install, no console commands), when I use my tool for the first time I get this (blue) error in the console:

" weapons\gmod_tool\stools/mass.lua:10: attempt to call method ‘PrintMessage’ (a nil value) "

In an attempt to recitfy this problem, I ran “lua_reloadents” in the console, and now the tool works as expected! Does anyone know why this happens?
Thanks in advance,
Danilo.

If I had to guess, your only doing it in single player, have you tried creating a multiplayer game instead?

Tried it in a random multiplayer sandbox map, got this message instead:

" weapons\gmod_tool\stools/mass.lua:10: Tried to use invalid object (type IPhysicsObject) (Object was NULL or not of the right type) "

Your LeftClick-function is currently being run both server- and clientside. Because player.PrintMessage is a serverside function, you would want to add

[lua]if CLIENT then return end[/lua]

on top of your function, so that it only runs serverside.

Unfortunately this still doesn’t resolve the problem! Code currently stands at:


TOOL.Category = "Danilo's Tools"
TOOL.Name = "Object Mass"
TOOL.Command = nil
TOOL.ConfigName = ""

function TOOL:LeftClick(trace)
if CLIENT then return end
if not trace.Entity then return end
local physobj = trace.Entity:GetPhysicsObject()
if physobj then
self.Owner:PrintMessage(HUD_PRINTTALK, physobj:GetMass())
end
end

When I load the game and use my tool for the first time, this is the blue error I get in the console:
weapons\gmod_tool\stools/mass.lua:11: attempt to call method ‘PrintMessage’ (a nil value)

Still after using lua_reloadents in the console, my stool (code above) works fine! This is boggling my mind.

PrintMessage is shared, what you said doesn’t work.

[lua]TOOL.Category = “Danilo’s Tools”
TOOL.Name = “Object Mass”
TOOL.Command = nil
TOOL.ConfigName = “”

function TOOL:LeftClick()
local ply = self:GetOwner()
local tr = ply:GetEyeTrace()
if (!tr.Entity) then return false end
if (!tr.Entity:IsValid() ) then return false end
if (tr.Entity:IsPlayer()) then return false end
if (tr.Entity:IsWorld()) then return false end

if ( CLIENT ) then return true end

local physobj = tr.Entity:GetPhysicsObject()
if physobj then
ply:ChatPrint(math.Round(tonumber(physobj:GetMass())))
end
end
[/lua]

Bluebarrel
59.999996185303
Rounded
59
Fixed no errors

Thanks Cubar, that seems to work! I still don’t really have any idea how that works, or why it didn’t work before, though. Now, need to plan my next stool…

Fixed no errors.