I’m currently building a round system from scratch as my first real “big” system/project, and as I was testing everything out, I stumbled upon a bug where my “inLobby” tracker isnt tracking properly. If one player leaves the count goes down in a weird order and often by multiple people, which then throws off the whole count.
Example: This is after one player left.
My Code:
local runService = game:GetService("RunService")
local INTERMISSION_TIME = 15
local ROUND_TIME = 30
local MIN_PLAYERS = 2
local activePlayers = {} -- In Game
local inLobby = {} -- In Lobby
local connection
connection = runService.Heartbeat:Connect(function()
print(#inLobby)
end)
game.Players.PlayerAdded:Connect(function(plr)
table.insert(inLobby, plr) -- Insert joined player into lobby table
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i, plrLeft in inLobby do
if inLobby[i] == plrLeft then
table.remove(inLobby, i)
print("Removed "..plrLeft.Name.." from the table.")
end
end
end)
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local INTERMISSION_TIME = 15
local ROUND_TIME = 30
local MIN_PLAYERS = 2
local activePlayers = {} -- In Game
local inLobby = {} -- In Lobby
local connection:RBXScriptConnection
connection = RunService.Heartbeat:Connect(function()
print(#inLobby)
end)
Players.PlayerAdded:Connect(function(plr)
table.insert(inLobby, plr) -- Insert joined player into lobby table
end)
Players.PlayerRemoving:Connect(function(plr)
local needle = table.find(inLobby,plr)::number?
if not needle then return end
table.remove(inLobby,needle)
end)
Sorry lol, I was trying to figure out how to remove the specific player who left from the table and ended up finding a post on the DevForum with some guy doing it similar to how I did it in the original script I posted. Anyways, thanks so much for the help, I’ve never used table.find or number and the colons yet so I’m definitely gonna research those. Again thanks for the help!
You can also use a dictionary, and honestly? Dictionary would be even more optimized in some cases + ability to store additional arguments.
Although you lose the ability to get player count, so, uhh, yeah, it’s fixable with having an outside counter tracking it, though.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local INTERMISSION_TIME = 15
local ROUND_TIME = 30
local MIN_PLAYERS = 2
local activePlayers = {} -- In Game
local inLobby:{[Player]:true} = {} -- In Lobby
local CountTrack:number = 0
local connection:RBXScriptConnection
connection = RunService.Heartbeat:Connect(function()
print(CountTrack)
end)
Players.PlayerAdded:Connect(function(plr)
CountTrack+=1
inLobby[plr]=true -- Insert joined player into lobby table
end)
Players.PlayerRemoving:Connect(function(plr)
CountTrack-=1
inLobby[plr]=nil
end)
Ohhh okay, I think I kinda get it now. I’ve seen people mention dictionaries before but never actually used one, so this helps a lot. The way you explained it makes more sense, I didn’t realize you could store stuff like that for each player. The counter thing seems a bit confusing still, but I’ll probably understand it better once I mess around with it in Studio.
Thanks for the example too, seriously. Gonna try playing around with dictionaries a bit and see if I can get it working. Really appreciate the help man.