Game keeps running when player leaves the game, even if all other players died

Hi!

Me and a friend are trying to make a round-based survival game but we’ve recently ran into a brick wall. When a player leaves the game while in a round, the clock just keeps counting down even when all other players have died and are in the lobby. I assume it has something to do with the table not updating correctly when someone leaves the game. So far we’ve tried to fix it with PlayerRemoving but no luck so far (Neither of us are scripting wonders and most of the scripting was done by someone who’s no longer on the team).

game:GetService('Players').Player:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild('Humanoid').Died:connect(function()
			local Update = {}
			for i, v in ipairs(_G.gameplayers)  do
				if v ~= player.Name or player 
					table.insert(Update,1,v)
				end
				_G.gameplayers = Update
	        end
		end)
	end)
end)

I think I’m missing some code (PlayerRemoving?) that updates the table when a player leaves, I just don’t know what or how to implement it.

Your script uses global variables, so without the other scripts that those variables connect to this will be hard to fix

but you could try the same code but replace PlayerAdded with PlayerRemoving and then change table.insert to table.remove

Thank you for your response. I’ve already tried replacing CharacterAdded/table.insert with PlayerRemoving/table but no result so far. Here’s the main script that it connect with.

local Tick = game.Workspace.SFX.tick
local Tada = game.Workspace.SFX.tada
local Startup = game.Workspace.SFX.startup
local Lose = game.Workspace.SFX.gameover
local Begin = game.Workspace.SFX.begin

local LoadSong = game.Workspace.Songs.MULE
local Caramell = game.Workspace.Songs.Caramelldansen

local Text
local replicatedstorage = game:GetService('ReplicatedStorage') 
local status = replicatedstorage:WaitForChild('StatusValue')
function setText (word)
	Text = word
	for i = 1, #Text do
		status.Value = string.sub(Text, 1, i)
		wait(0.02)
	end
end
local mapstorage = game.Workspace:WaitForChild('mapStorage')
while true do
	
while game.Players.NumPlayers < 1 do
		status.Value = "Initialising"
		repeat wait(2) until game.Players.NumPlayers >= 1
end
Caramell:Stop()
LoadSong:Play()
game.Workspace.RoundEnd.Value = false
for i = 10,0,-1 do
		status.Value = "Intermission... "..i
		Tick:Play()
	wait(1)
end
_G.gameplayers = {}
for i, v in pairs(game.Players:GetPlayers()) do

if v then
	table.insert(_G.gameplayers, v.Name) --Players get inserted in the table here. Maybe there's something missing here?
end

end



local mapsinserverstorage = game:GetService('ServerStorage'):GetChildren()
local chosenmap = mapsinserverstorage[math.random(1, #mapsinserverstorage)]
local workmap = chosenmap:Clone()
workmap.Parent = mapstorage
wait(1)
Tada:Play()
setText("Current map: "..chosenmap.Name)
wait(3)
setText("")
wait(1)
	
local rSound = game.Workspace.rSongs:GetChildren()
local Table = {}
for i=1,#rSound do
	if rSound[i]:IsA("Sound") then
		Table[#Table+1]=rSound[i]
	end
end
rSound = Table[math.random(1,#Table)]
rSound:Play()
	
local spawns = workmap:WaitForChild('Spawns'):GetChildren()
for _, player in pairs (game.Players:GetPlayers()) do
	if player and #spawns > 0 then
		local Torso = player.Character:WaitForChild('HumanoidRootPart')
		local allspawns = math.random(1, #spawns)
		local randomspawn = spawns[allspawns]
		if randomspawn and Torso then
			table.remove(spawns, allspawns)
				Torso.CFrame = CFrame.new(randomspawn.Position + Vector3.new(0,2,0))
				Torso.Parent.showtools.Disabled = false
			
			function fadeTo(a, b, c)
	for transparency = a, b, c do
 
		for _, part in pairs(player.Character:GetChildren()) do
 
			if part:IsA("BasePart") then

 
				part.Transparency = transparency
			end
		end
		wait(0.1)
	end
end
			
		end
	end
end
LoadSong:Stop()
setText("Begin!")
Begin:Play()	
wait(1)
	workmap.Game.Disabled = false
	for i = math.huge,0,-1 do 
		if i == 10 then
			Tick.PlaybackSpeed = 1.1
		end
	if i == 0 or game.Workspace.RoundEnd.Value == true then
			setText("Round end.")
			wait(3)
			for i, v in pairs(_G.gameplayers) do
			if v ~= nil then
				setText(v.." Survived.")
					Startup:Play()
					Caramell:Play()
					rSound:Stop()
					wait(30)
				break
			end
		
		end
		break
    end
	wait(1)
	if #_G.gameplayers == 0 then
		for i, v in pairs(_G.gameplayers) do
			if v ~= nil then
				break
			end
		
			end
			workmap.Game.Disabled = true
			setText("Everyone died!")
			Lose:Play()
			rSound:Stop()
			wait(15)
        break
	end
end
Tick.PlaybackSpeed = 1
	mapstorage:ClearAllChildren()
	game.Workspace.mapObjects:ClearAllChildren()
wait()
for i, player in ipairs(game.Players:GetPlayers()) do
	if player.Character then
		local hum = player.Character:FindFirstChild('Humanoid')
		if hum then
			player:LoadCharacter()
		end	
end
end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.