Attempting to use [self]

Hello!

Im trying to learn how to use [self], could anyone elaborate on why this doesn’t work/help me make it work?

local safeZone = {}

safeZone.Bag = {"Fortnite"}

function safeZone:updateSafezone()
    print(self.Bag)
end

return safeZone

result:

attempt to index nil with 'Bag'
1 Like

I think you’re trying to create a class. That should be done as follows:

local safeZone = {}
safeZone.__index = safeZone

function safeZone.new()
    local newSafeZone = {
        Bag = "Fortnite"
    }

    return setMetatable(newSafeZone, safeZone)
end

function safeZone:updateSafezone()
    print(self.Bag)
end

return safeZone

Only now you’ll be able to perform safeZone:updateSafezone().

Read here for further information:

1 Like

This was the solution!

Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.