Setting a field to nil does not actually set it to nil?

I’m encountering some very confusing behavior. In my code, I have a class called ServerSession with a method remove_entity(). Inside this method I assign the entity field to nil, like so:

function ServerSession:remove_entity()
  --[[
    Removes the reference to the session's entity (so a new
    one can be loaded).
  --]]

  if not self.entity then
    warn("Attempted to remove entity from session with no entity")
  end

  self.entity = nil

  print(self.entity)
end

However, when I actually run the code, it prints table: 0xaf0b4d18e72e54bc, which means the field was not actually set to nil. This is very strange behavior and I couldn’t really find any similar cases online. I would greatly appreciate any help.
Some more information: this method is invoked on an event, like so:

session.entity.events.died:Connect(function()
    session:remove_entity()
  end)

So I’m thinking that may be the problem, but I’m not sure why. Everything else about my class, including other fields, work fine.

Unable to repro.

local Test = {}
Test.__index = Test

function Test.new()
    return setmetatable({
        entity = {}
    }, Test)
end

function Test:Get()
    return self.entity
end

function Test:Remove()
    print("Existing", self.entity)

    self.entity = nil

    print("Current", self.entity)
end

local A = Test.new()
print("Get", A:Get())
A:Remove()
Get	table: 0x8e58c0
Existing	table: 0x8e58c0
Current	nil

I’m running this from the Lua Demo website though so my test could be wildly inaccurate. Do you have any other code in remove entity, references to self.entity or any overriding methods? Have you tried waiting a bit before printing self.entity (though that shouldn’t be necessary)?

There’s a lack of code to address this issue properly. From what’s been shown so far, this shouldn’t be producing any kind of issue, which leads me to believe the problem lies elsewhere.

3 Likes

I know it looks strange, and I agree this seems like there should be an error somewhere else. I feel like I might be going insane right now. I will try to isolate the problem into a reproducible portion when I have the time for you guys.
The class does not inherit from another class, so it doesn’t override any methods. My remove_entity code is exactly as I wrote it. The entity field is referenced by other parts of the code, but I don’t think that should affect it, since I’m literally setting the field to nil and printing it in the next line. I’ll spend some more time investigating my code and report back.

1 Like

If you’re using metatables make sure __newindex is working as intended. If it isn’t metatables it could maybe be a bug with the new VM.

1 Like

It turns out I actually was insane. In an innocent-looking line of code, I set the entity field of ServerSession to something, instead of the instance. This made it so the entity field of any instance could never be nil (since there was an entity element in the metatable). That’s what I get for trying to code this late in the evening ;(
Thanks for all your help everyone.

1 Like