Need help on fixing leave and join system on parts

I am trying to make a script that is a join and leave system. It is kind of hard to explain it, but I will give you an example.
Example:

The first player (Player 1) comes up to this clickable part and clicks it. The button gave the one who clicked it an animation then stops right after. Once the animation reaches about half way, a part inside the model of the clickable part which is above it, will glow (Change colours) Then another player comes up and clicks it (Player 2), it does the same animation and the same thing happens. Above the first players glowing part, for player 2 it makes the part a different colour also above player 1’s colour part. But Player 1/Player 2 leaves, so then the top colour part will go down 1. This continues until there are 5 players max.

In these images you can see what I am trying to say:




And I also need a part that makes the cancolide on if you are not in the match.

Here is the script I have so far: (I am somewhat new to scripting)

local clickDetector = script.Parent
local Players = game:GetService("Players")
local model = clickDetector.Parent.Parent

local playerParts = {}
for _, child in model:GetChildren() do
    if child:IsA("Part") and tonumber(child.Name:match("%d+")) then
        table.insert(playerParts, child)
    end
end

local clickCount = {}
local isAnimating = false

local function updateGlowState(player)
    local color = Color3.fromHSV(0.6, 1, 1)
    for _, part in playerParts do
        part.Color = color
    end
    clickCount[player] = (clickCount[player] or 0) + 1
end

local function onClick(player)
    if clickCount[player] and clickCount[player] > 0 then return end
    
    clickCount[player] = 1
    isAnimating = true
    
    local humanoid = player.Character and player.Character:FindFirstChild("Humanoid")
    if humanoid then
        local animator = humanoid:FindFirstChild("Animator")
        if animator then
            local animation = Instance.new("Animation")
            animation.AnimationId = "rbxassetid://140413569393737"
            local track = animator:LoadAnimation(animation)
            track:Play()

			wait(2)
			track:Stop()
            isAnimating = false
            updateGlowState(player)
        end
    end
end

local function onPlayerLeave(player)
    clickCount[player] = nil
    if next(clickCount) == nil then

        for _, part in playerParts do
            part.Color = Color3.new(0, 0, 0)
        end
    end
end

clickDetector.MouseClick:Connect(function(player)
    onClick(player)
end)

Players.PlayerRemoving:Connect(function(player)
    onPlayerLeave(player)
end)


for _, player in Players:GetPlayers() do
    clickCount[player] = 0
end

If you can help me with this issue I will be very greatful.

I forgot to say, I also need the script to disable a script if there is 1 or more players in the match. If there is 0 players I need it Enabled.

I misread this post a few times so I might’ve missed a part or two. Let me know.

  1. Adding a queue system
    Queue systems could be implemented simply using tables.
  • Simply add a clickdetector to the queue button. Then, ClickDetector.MouseClick:Connect(function(player) end)
  • Within the function() parameter, you will need to add your code. In this case, you should table.insert the player variable into a table you create.
  • If they already are in the table (use table.find) they will be table.remove() instead. Since you need an index to remove, you could loop through the table until (assuming i,v in pairs) v == player then remove i from the table.
  • Otherwise, make sure that # length of the table is less than five #table<5; if not then return end.
  • As for the visual display, you could just make a BillboardGui or TextLabel that displays the string #table.."/5 players" which essentially combines the length into a string type.
    • I skimmed through your code and see you need a glow effect too, I’ll cover this in the next section.
  1. Glow State
  • Name each glow part to the number of players required to light it up.
    • i.e. ring 1 Name = "1" etc.
    • Store these someplace you can access easily
  • You’ll need to use #table again. Simply make the place you stored your glow rings a variable.
  • for _, ring in glowringstorage:GetChildren() do
  • Now we need to check if ring.Name <= #table.
  • If it is, then you should change the part color.
  • Otherwise, you should change the part color back to white (or default)
  • Close the loop.

This should cover all your questions. Let me know if you have any more.

Can you insert it into the script that I already have?

Unfortunately I am unavailable to do so, but you can ask AI to compile it for you.

  • Try copy pasting my message then your code with instruction

Please mark the solution if you have no more questions, thanks!

I tried that multiple times and it never works