Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

player amount displaying script works only when 2 players leave?

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?

5 comments

0

Is this a LocalScript, or Script?

0

script

0

it works when a player is added tho

0

If it is a LocalScript, it won't detect the LocalPlayer being added.

0

its a normal script

1 answer

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)

4 comments

0

I forgot to remove some print() while I was testing it. Remember to remove them!

0

There will never be a case where you have to verify the Player Object on the Client.

0

Use :GetPlayers().

0

I think I should avoid changing the original script?

This discussion is preserved as it was. New answers, comments, and votes are closed.