Hello, I'm trying to make a multiplayer handler (Server Script) that keeps track of the numbers of players in the game (Max is 4) By assigning them a value that is stored in an array and removed from said array when they leave. However, I seem to be having trouble thus far... The goal is to remove the initial player value from the array when a player leaves, then when a player is joining, detect where the player should go in the array. Here is my code:
local plrCount = {} local plrsToHave = 4 game.Players.PlayerAdded:Connect(function(plr) local char = plr.Character or plr.CharacterAdded:Wait() local playerV = Instance.new("NumberValue") playerV.Name = "playerV" playerV.Parent = plr counter = counter + 1 playerV.Value = counter table.insert(plrCount, playerV.Value) notify:FireAllClients("Waiting for " .. plrsToHave - #plrCount .. " more players.", 10) -- fire to prompt client telling everyone how many players have to join for the game to start local playerV = plr:FindFirstChild("playerV") if counter > 4 then for i, player in pairs(Players:GetChildren()) do local pV = player:WaitForChild("playerV") if pV.Value == playerV.Value then table.remove(plrCount, playerV.Value) local total = (#plrCount + 1)*(#plrCount + 2)/2 local sum = 0 for i,v in pairs(plrCount) do sum = sum + v end local missing = total - sum playerV.Value = missing table.insert(plrCount, missing) -- re instert player end end end if #plrCount == plrsToHave then gameDidStart() end end) game.Players.PlayerRemoving:Connect(function(plr) counter = 0 local playerV = plr:FindFirstChild("playerV") table.remove(plrCount, playerV.Value) end)
Any suggestions or comments on how to fix this would really help!