Dynamic Intermission Time Logic Issue

Hey guys, in my current project, I’m trying to achieve a dynamic intermission time based on player count.

As an example:-
If the player count = 2, intermission time = 10
Player count = 3, intermission time = 15

I’m having trouble with certain logic in taking into account player leaving. This is how I did mine to explain the flow.

Play Pad

Player Count Int Value

-- Script in server script storage
-- Players enter play pad to go into the arena
-- Using zone module to handle touch
-- Adding participating players into a table
local isMatchStart = ReplicatedStorage:WaitForChild("IsMatchStart")
local playerCount = ReplicatedStorage:WaitForChild("PlayerCount")
local playersInMatchTable = {}
local MIN_PLAYERS = 2

zonePlay.playerEntered:Connect(function(player)
	if not MatchControllerScript:GetAttribute("IsIntermissionOver") then
		-- Teleporting player to arena code here

		table.insert(playersInMatchTable, player)
		playerCount.Value = #playersInMatchTable
		print(playersInMatchTable)
	end
end)

playerCount.Changed:Connect(function()
	if playerCount.Value >= MIN_PLAYERS then
		-- Match starting trigger
		isMatchStart.Value = true
	end
end)
-- In other script that handle match/level control
local isMatchStart = ReplicatedStorage:WaitForChild("IsMatchStart")
local playerCount = ReplicatedStorage:WaitForChild("PlayerCount")
local TIMER_MULTIPLIER = 5

isMatchStart.Changed:Connect(function()
	if isMatchStart.Value then
		-- Dynamic match countdown based on player count
		local countdown = TIMER_MULTIPLIER * playerCount.Value
		playerCount.Changed:Connect(function()	
			countdown += TIMER_MULTIPLIER
		end)

        -- While loop timer below..
    else
        -- Match ends
    end
end)

The problem with playerCount.Changed is that I have functions that also remove player from the table if the player left the game in the middle of it, and as a result, the countdown timer also increases.

How would I go about to not increase the timer when the player left or only for players that joined?

You can calculate the difference and do some maths with it:

isMatchStart.Changed:Connect(function()
	if isMatchStart.Value then
		-- Dynamic match countdown based on player count
        local playerCount = playerCount.Value
		local countdown = TIMER_MULTIPLIER * currentPlayerCount
		playerCount.Changed:Connect(function()
            local currentPlayerCount = playerCount.Value
            local playerCountDifference = currentPlayerCount - playerCount
			countdown += TIMER_MULTIPLIER
            playerCount = currentPlayerCount
		end)

        -- While loop timer below..
    else
        -- Match ends
    end
end)
1 Like

Thank you. Was having some difficulty wrapping my head around it, basic maths and logic if the count difference is either 1 or -1. Marked as solution. The full code below for any future reference.

isMatchStart.Changed:Connect(function()
	if isMatchStart.Value then
		-- Dynamic match countdown based on player count
        local prevPlayerCount = playerCount.Value
		local countdown = TIMER_MULTIPLIER * currentPlayerCount
		playerCount.Changed:Connect(function()
            local currentPlayerCount = playerCount.Value
            local playerCountDifference = currentPlayerCount - prevPlayerCount
			if playerCountDifference > 0 then
				print("Countdown timer increased")
				countdown += TIMER_MULTIPLIER			
			end
            prevPlayerCount = currentPlayerCount
		end)

        -- While loop timer below..
    else
        -- Match ends
    end
end)