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