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.
-- 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?