You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
So I have to lobbies table where the owner would be the key and it would represent another table with all the player thats in his lobby including the owner himself so ex: [“plr1”] = {plr1, plr2} where plr1 would be the owner. -
What is the issue? Include screenshots / videos if possible!
The issue happens when there are three players lets say plr1, plr2 and plr3. Plr1 creates the lobby with plr2 joining followed by plr3. Normally if the owner(plr1) leaves then it would remove plr1 as the key and from the table and then it would choose a random owner between plr2 and plr3. The new owner would then be set as that key. Everything works as intended if it chooses plr2 which is the first one that joined the lobby but the bug seem to be reproduced 100percent of the time when plr3(the last one that joined) is chosen. It would basically skip the part where he is removed from the lobbyslots and PlrPosition would be nil. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I honestly don’t know how to fix this bug so far but I think I know where the bug is happening
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local lobbies = {}
--Functions
local function addLobby(player, Difficulty)
for lobbyindex, lobbyslot in pairs(lobbies) do
if lobbyindex == player.Name then
createlobby:FireAllClients(Difficulty, lobbyindex)
break
end
end
end
local function findkeyfromPlayer(player) --This function is made so I don't have to use owner parameter
for lobbyindex, lobbyslots in pairs(lobbies) do
for index, players in pairs(lobbyslots) do
if players == player then
return lobbyindex
end
end
end
end
local function playerleaveLobby(player)
local lobbyindex = findkeyfromPlayer(player)
local PlrPosition = nil
if not lobbyindex then
return
end
local lobbyslots = lobbies[lobbyindex]
for index, players in pairs(lobbyslots) do
if players == player then
PlrPosition = index
table.remove(lobbyslots, index)
break
end
end
return PlrPosition, lobbyslots --Update here
end
local function ownerleaveLobby(player)
local lobbyindex = findkeyfromPlayer(player)
local TheNewOwner = nil
local PlrPosition = nil
if not lobbyindex then
return
end
local lobbyslots = lobbies[lobbyindex]
print(lobbyslots)
for index, players in pairs(lobbyslots) do --The problem is here
if #lobbyslots >= 2 then
if players == player then
print("Owner removed from inlobby")
PlrPosition = index
table.remove(lobbyslots, index)
end
local RandomSelect = math.random(1, #lobbyslots)
local NewOwner = lobbyslots[RandomSelect]
TheNewOwner = NewOwner
print(NewOwner)
print(lobbies)
lobbies[player.Name] = nil
lobbies[NewOwner.Name] = lobbyslots
newowner:FireAllClients(lobbyindex, NewOwner) --For mainmenu lobby gui
else
lobbies[player.Name] = nil
deletelobby:FireAllClients(player) --Update gui when lobby is gone
print("Lobbydelete")
end
break
end
return PlrPosition, TheNewOwner
end
--On Event Connect
requestlobby.OnServerEvent:Connect(function(player, Difficulty)
if not lobbies[player.Name] then
lobbies[player.Name] = {player}
end
addLobby(player, Difficulty)
end)
joinlobby.OnServerEvent:Connect(function(player, Owner) --REMEMBER OWNER IS THE NAME NOT AN INSTANCE
for lobbyindex, lobbyslots in pairs(lobbies) do
if lobbyindex == Owner then
table.insert(lobbyslots, player)
for i, players in pairs(lobbyslots) do
joinedplayerupd:FireClient(players, lobbyindex, lobbyslots) --Owner parameter not need most likely, just pass "lobbyindex" lmao
end
end
end
end)
leavelobby.OnServerEvent:Connect(function(player)
if not lobbies[player.Name] then
local PlrPosition, lobbyslots = playerleaveLobby(player)
if lobbyslots then
--do more here
end
else
local PlrPosition, NewOwner = ownerleaveLobby(player)
if NewOwner then
local lobbyslots = lobbies[NewOwner.Name]
if lobbyslots then
for index, players in pairs(lobbyslots) do
inlobbyguiupd:FireClient(players, PlrPosition, lobbyslots)
end
end
end
end
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.