Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

player amount displaying script works only when 2 players leave?

Asked by 3 years ago
Edited 3 years ago
game.Players.PlayerAdded:Connect(function()
    local minplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MinimumPlayers")
    local maxplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MaximumPlayers")
    script.Parent.Text = #game.Players:GetChildren().."/"..maxplr.Value.." Players"
    if #game.Players:GetChildren() < minplr.Value then
        script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    else
        script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    end
end)
game.Players.PlayerRemoving:Connect(function()
    local minplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MinimumPlayers")
    local maxplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MaximumPlayers")
    script.Parent.Text = #game.Players:GetChildren().."/"..maxplr.Value.." Players"
    if #game.Players:GetChildren() < minplr.Value then
        script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    else
        script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    end
end)

script above is supposed to show current player amount for example: 3/10 Players or 8/10 Players. pretty simple but for some reason it only works when 2 people leave, does anyone know whats going on?

0
Is this a LocalScript, or Script? NotTheChara 191 — 3y
0
script TFlanigan 86 — 3y
0
it works when a player is added tho TFlanigan 86 — 3y
0
If it is a LocalScript, it won't detect the LocalPlayer being added. NotTheChara 191 — 3y
0
its a normal script TFlanigan 86 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I simulated what is happening to you and I saw two problems. First, the newest player will not get the 3/10 Players or 8/10 Players thing. Second, it only update once when 2 people leave. Here's how to solve that.

First, you should use LocalScript to update Guis, here's what you should do in the first line so we can get the LocalPlayer.

local player = game.Players.LocalPlayer

Then, we check if we can get the player and do what you want to do.

local player = game.Players.LocalPlayer

if player then
    print("Player Detected")
    local minplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MinimumPlayers")
    local maxplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MaximumPlayers")
    script.Parent.Text = #game.Players:GetChildren().."/"..maxplr.Value.." Players"
    if #game.Players:GetChildren() < minplr.Value then
        script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    else
        script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    end
end

For the second problem, we can use ChildRemoved to solve that since PlayerRemoving fires when the player is removing, not removed which means the player is still here, but if we use ChildRemoved, we detect when the player is removed completely. Here's the code.

local player = game:GetService("Players").LocalPlayer

if player then
    print("Player Detected")
    local minplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MinimumPlayers")
    local maxplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MaximumPlayers")
    script.Parent.Text = #game.Players:GetChildren().."/"..maxplr.Value.." Players"
    if #game.Players:GetChildren() < minplr.Value then
        script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    else
        script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    end
end
game:GetService("Players").ChildRemoved:Connect(function()
    print("Player Removing")
    local minplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MinimumPlayers")
    local maxplr = game.ReplicatedStorage.CustomValues:FindFirstChild("MaximumPlayers")
    script.Parent.Text = #game.Players:GetChildren().."/"..maxplr.Value.." Players"
    if #game.Players:GetChildren() < minplr.Value then
        script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    else
        script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
        script.Parent.TextStrokeColor3 = Color3.fromRGB(0, 0, 0)
    end
end)
0
I forgot to remove some print() while I was testing it. Remember to remove them! NotTheChara 191 — 3y
0
There will never be a case where you have to verify the Player Object on the Client. Ziffixture 6913 — 3y
0
Use :GetPlayers(). Ziffixture 6913 — 3y
0
I think I should avoid changing the original script? NotTheChara 191 — 3y
Ad

Answer this question