I am attempting to fix a glitch in my game, the glitch in question happens when the code below is being executed when a player leaves the game, I have attempted to remedy this by adding a check to ensure all players are in the server before proceeding (lines 5-8), however, said check is faulty and will not work. Any advice? (For those wondering, Ghame is the game loop, which includes the code below.)

local players = randomizePlayers(game.Players:GetChildren())        
        local spawns = map.SpawnPoints:GetChildren()
        for i = 1, #players do
            local v = players[i]
            if readyPlayers > #players
            then
                Ghame()
            end
            v.Character:MoveTo(spawns[i].Position)
            for _, x in pairs(game.ServerStorage.Weapons:GetChildren()) do
                x:Clone().Parent = v.Backpack
            end
            v.Playing.Value = true
        end

the code below are two snippets from the script that define readyPlayers, Playing, and randomizePlayers, if needed.

function readyPlayers()
	return #(game.Players:GetChildren())
end

function randomizePlayers(pList)
	local newPList = {}
	for i = 1, #pList do
		local num = math.random(#pList)
		print(pList[num])
		table.insert(newPList, pList[num])
		table.remove(pList, num)
	end
	return newPList
end

game.Players.PlayerAdded:connect(function(player)
	local inValue = Instance.new("BoolValue", player)
	inValue.Name = "Playing"
end)

1 comment

0

We need all information affiliated with this code, that includes the definitions of variables such as readyPlayers. Please provide everything necessary.