Accounting for Player Leave on a Roundbased System

hey guys, I want your suggestions for what to do in my round-based system. What I want to achieve is a round-based system bomb that explodes in x amount of seconds. After the bomb explodes the repeat loop will start again and give the bomb to another player if players are not 1. the problem is for example if one player leaves it to wait until the bomb explodes to check again.

I removed parts of the script that are not useful to the solution afaik. I also have a characterremoving event that removed the player from the playing and if he was the bomber it would pick another random player if there are enough players but if not it would just destroy the bomber value.

	repeat		
		local Bombs = colService:GetTagged('Bomb')
		local Timer = 30

		local function TickSecond()
			local start = tick()
			repeat
				runService.Heartbeat:Wait()
			until tick() - start >= 1
		end

		repeat
            CountVal.Value = Timer
			TickSecond()
			Timer -= 1		
		until Timer <= 0

		for i,Bomber in pairs(Bombers:GetChildren()) do
			for i,v in pairs(Playing) do
				if v == Players:GetPlayerFromCharacter(Bomber.Value) then
					table.remove(Playing,i)
				end
			end
			Bomber:Destroy();
		end
		
	until #Playing < 1

I have a few things I want to address with this topic. First being the TickSecond function. That might have been a better alternative awhile back, but ever since the task library came out, task.wait should be fine (even more so since it’s 1 second). Secondly, you could just use CollectionService to tag the player with the bomb.

while #Playing > 2 do -- we want 1 winner
    local bombs = colService:GetTagged("Bomb")
    local timer = 30
    
    for i = timer, 0, -1 do
        CountVal.Value = i
        task.wait(1)
        
        if #Playing <= 1 then
            return
        end
    end

    for i,Bomber in pairs(Bombers:GetChildren()) do
		for i,v in pairs(Playing) do
			if v == Players:GetPlayerFromCharacter(Bomber.Value) then
				table.remove(Playing,i)
			end
		end
		Bomber:Destroy();
	end
end

local winner = Playing[1] -- should only be 1 player left in the table

As for detecting if a player leaves, remove them from the Playing table (if found inside of it).

local function onPlayerRemoving(player)
    if table.find(Playing, player) then
        table.remove(Playing, table.find(Playing, player)
    end
end
1 Like