local players = game:GetService("Players")
local lobby = {}
local mt = {}
mt.__index = mt
function lobby.new(zone_model: Model)
local self = {}
setmetatable(self, mt)
self.instance = zone_model
self.level_needed = zone_model:GetAttribute("LevelNeeded")
self.place_id = zone_model:GetAttribute("PlaceId")
self.players = setmetatable({}, {
__newindex = function(t, player: Player, exists: boolean)
print("newindex")
if exists then
local leave_lobby_gui = script.LeaveLobbyGUI:Clone()
leave_lobby_gui.Parent = player.PlayerGui
leave_lobby_gui.Container.LeaveButton.MouseButton1Click:Once(function()
print("clicked")
self:remove_player(player) --> This is recursive because this function will be called, but exists boolean will be nil.
end)
else
--> This will run if the leave button is clicked, or manually called by the server.
local leave_lobby_gui = player.PlayerGui:FindFirstChild("LeaveLobbyGUI")
if leave_lobby_gui then
leave_lobby_gui:Destroy()
end
end
rawset(t, player, exists)
return t
end,
})
local zone_area = zone_model.Zone :: BasePart
zone_area.Touched:Connect(function(part_hit)
self:add_player(players:GetPlayerFromCharacter(part_hit.Parent))
end)
end
function mt:add_player(player: Player)
if not player then return end --> Checking if player exists.
self.players[player] = true
end
function mt:remove_player(player: Player)
if not player then return end --> Checking if player exists.
self.players[player] = nil
end
return lobby
This works when Touched is fired, but inside where I remove the player from the list by clicking the button just doesn’t fire the newindex function. I’m new to metatables.
The add and remove player are easy functions.
function mt:add_player(player: Player)
if not player then return end --> Checking if player exists.
self.players[player] = true
end
function mt:remove_player(player: Player)
if not player then return end --> Checking if player exists.
self.players[player] = nil
end