Push the Button - KindOf game, bugs out if +4 players are on

Hi guys!

I am currently making a game like “Don’t push the button”.
Everything works (From teleporting the players, choosing the event, updating the time remaining & stuff).

The problem is the following:
I’ve added aProximityPrompt instead of clickdetector get the button spammed (otherwise you can spam the button and it will get X numbers of events, teleporting everyone to the next event over and over again, without having time to do anything), but still it doesn’t do the job, so I removed the Proximity prompt and added the clickdetector back

BUT, if there are more players in the server and they spawn when an event is running, they can press the button and it will teleport everyone (including the ones that are already in an event) bugging the server out.

Can someone tell me / help me with it a bit ? I just want the button to be accessible only if the previous event ended and the deleting of the map & assets finished.
And for the players that just connected to the server, to also wait for the round to be finished.

I know the script is a mess, I tried to make the full handler in one script

local eventDurations = {
    [1] = 60,  -- Event 1 (Sword Fight)
    [2] = 30,  -- Event 2 (Temple Run)
    [3] = 40,  -- Event 3 (Acid Escape)
    [4] = 70,  -- Event 4 (Conveyor Chaos)
    [5] = 60,  -- Event 5 (Snowball Dodge)
    [6] = 45,  -- Event 6 (Tsunami Escape)
    [7] = 70,  -- Event 7 (Ghost Hunt)
    [8] = 70,  -- Event 8 (Color Grid)
    [9] = 70,  -- Event 9 (Red/Green Light)
    [10] = 30  -- Event 10 (Speed Run)
}

local lastEvent = nil
local isEventActive = false  -- Flag to track event activity

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    -- Check if an event is already in progress
    if isEventActive then
        warn("An event is already in progress! Please wait until it ends.")
        return
    end

    -- Set isEventActive to true so no other event starts during this one
    isEventActive = true
    game.ReplicatedStorage.EventValue.Value = ""
    game.ReplicatedStorage.PlayerValue.Value = ""
    game.ReplicatedStorage.TimeRemaining.Value = 0
    local button = script.Parent
    local buttonPart = game.Workspace.Button["Animated Part"]

    -- Ensure player exists
    if not player or not player:IsA("Player") then
        warn("Error: Player instance is invalid.")
        return
    end

    -- Get player's leaderstats
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local panicTimes = leaderstats:FindFirstChild("🚨 Panic Times")
        if panicTimes then
            panicTimes.Value = panicTimes.Value + 1
            print(player.Name .. " has gained 1 Panic Time! Total: " .. panicTimes.Value)
        else
            warn("🚨 Panic Times stat not found for player: " .. player.Name)
        end
    else
        warn("Leaderstats folder not found for player: " .. player.Name)
    end

    -- Store player name for tracking
    game.ReplicatedStorage.PlayerValue.Value = player.Name

    -- Pick a new event, ensuring it's different from the last one
    local availableEvents = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}  -- Added event 10 (Speed Run Challenge)
    if lastEvent then
        for i, event in ipairs(availableEvents) do
            if event == lastEvent then
                table.remove(availableEvents, i)
                break
            end
        end
    end
    local event = availableEvents[math.random(#availableEvents)]  -- Pick a random event
    lastEvent = event

    print("Event triggered: " .. event)

    local eventDuration = eventDurations[event] or 30
    game.ReplicatedStorage.TimeRemaining.Value = eventDuration
    local teleportPoint = nil
    local eventMap = nil
    local winnerPart = nil

    -- Start timer for the event (update for all players)
    task.spawn(function()
        while game.ReplicatedStorage.TimeRemaining.Value > 0 do
            task.wait(1)

            -- Check if 3 seconds are remaining and reward the players
            if game.ReplicatedStorage.TimeRemaining.Value == 3 then
                -- Award PTokens to players just before the event ends
                for _, p in pairs(game.Players:GetPlayers()) do
                    local leaderstats = p:FindFirstChild("leaderstats")
                    if leaderstats then
                        local pTokens = leaderstats:FindFirstChild("🎟️ PTokens")
                        if pTokens then
                            local rewardAmount = (event == 1 and 50) or (event == 2 and 25) or (event == 3 and 75) or 
                                (event == 4 and 75) or (event == 5 and 50) or (event == 6 and 50) or
                                (event == 7 and 50) or (event == 8 and 75) or (event == 9 and 50) or 
                                (event == 10 and 75)
                            pTokens.Value = pTokens.Value + rewardAmount
                            print(p.Name .. " has been awarded " .. rewardAmount .. " PTokens!")
                        end
                    end
                end
            end

            -- Update the time remaining
            game.ReplicatedStorage.TimeRemaining.Value = game.ReplicatedStorage.TimeRemaining.Value - 1
        end
    end)

    -- Update UI for all players
    for _, otherPlayer in pairs(game.Players:GetPlayers()) do
        local playerGui = otherPlayer:FindFirstChild("PlayerGui")
        if playerGui then
            local eventsUI = playerGui:FindFirstChild("EventsUI")
            if eventsUI then
                local timeLabel = eventsUI:FindFirstChild("TimeLeft")
                if timeLabel then
                    task.spawn(function()
                        while game.ReplicatedStorage.TimeRemaining.Value > 0 do
                            timeLabel.Text = "Time Left: " .. game.ReplicatedStorage.TimeRemaining.Value .. "s"
                            task.wait(1)
                        end
                        timeLabel.Text = "Time Left: 0s"
                    end)
                end
            end
        end
    end

---- HERE ARE THE 10 EVENTS ( IF EVENT == 1 THEN, cloning the map and the assets, teleporting the players) and so on

 - The code from below is right after the events (making the button clickable again and making sure the map is deleted

    -- Wait for event duration to end
    wait(eventDuration + 3)

    -- Remove the event map if all players die
    if eventMap then
        eventMap:Destroy()
    end

    -- Re-enable button after event ends
    button.Transparency = 1
    button.ClickDetector.MaxActivationDistance = 32
    buttonPart.Transparency = 0
    isEventActive = false  -- Allow the button to be pressed again
    game.ReplicatedStorage.ButtonReactivationEvent:FireAllClients()  -- Notify all clients to re-enable the button
end)