• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Lua self.<fieldName> not working as expected (1 Viewer)

keys9

Newcomer
Joined
Nov 5, 2021
Messages
1
Reaction score
0
Hello, I am new to Roblox coding and have a question about why I am getting the following error when I run the test server:
Code:
 isReady is not a valid member of Player "Players.Player1"

I omitted what I believe to be irrelevant parts of the code. Basically when BoardQueue:addPlayer is called I get the above error. I expect it to print the value of isReady in the instance of my BoardQueue class but instead I get that error. I am very confused because I am using self in a : function with the name of the class I Want self to refere to in front of the : . Forgive me if this is a newbie question as I am new to Lua.

Code:
local BoardQueues = {}
local playersInQueue = {}


local JoinBoardQueue = rStore:WaitForChild("JoinBoardQueueNow")

local function enqueuePlayer(player, board)
   
    if(playersInQueue[player]) then
        print("Player is already in queue")
        return
    end
    local queue = getQueueForBoard(board)
    if(queue == nil) then
        --if no queue was found, make one, player will be added to queue by constructor
        local newQueue = BoardQueue:new(nil, boardName, player)
        table.insert(BoardQueues, newQueue)
        newQueue.addPlayer(player)
    else
        --otherwise insert them into existing queue
        queue.addPlayer(player)
    end
   
    addToQueue(player)
   
    return board
end

JoinBoardQueue.OnServerInvoke = enqueuePlayer

--Meta class
BoardQueue = {players, boardName, leader}

--Derived class method new
--BoardQueue holds all players in queue and keeps track of whether or not it is full
function BoardQueue:new(o, boardName, leader)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    local newTable = {}
    table.insert(newTable, leader)
    self.players = newTable --list of queued players including leader
    self.isReady = false --whether the queue is ready to start a game
    self.boardName = boardName --The name of the party board
    self.queueLeader = leader --The queue leader. Leader decides when to start if less than 4 players present in queue
    return o
end

function BoardQueue:addPlayer(player)
    print("Ready ", self.isReady)
end
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
77
Reaction score
10
Because you defined the function 'addPlayer(player)' with the colon, but you called with just the dot
Lua:
newQueue.addPlayer(player) -- No
newQueue:addPlayer(player) -- Yes
If you don't know, what the colon does is adding an invisible parameter that is the table itself, basically:
Lua:
function table:func(param)
-- Is equal to
function table.func(self, param) -- Where self is the same 'table'
So if you do:
Lua:
function BoardQueue:addPlayer(player)
-- Is the same as:
function BoardQueue.addPlayer(self, player)
-- And then call it like this:
newQueue.addPlayer(player)
-- Lua interprets it like
newQueue.addPlayer(player, nil)
-- The param self is the player and the param player is nil.
 
Top