Help with GUI displaying leaderstat

local CoreGui = game:GetService("StarterGui")

CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

local function temp (player)
	local wins = player.leaderstats.wins.Value
	
	local frame = game.ReplicatedStorage.Template:Clone()
	frame.Parent = script.Parent.Frame.ScrollingFrame
	frame.Name = player.Name
	frame.name.Text = player.Name
	frame.Visible = true
end

game.Players.PlayerAdded:Connect(function(plr)
	temp(plr)
end)

for _,players in pairs(game.Players:GetChildren()) do
	temp(players)
end

game.Players.PlayerRemoving:Connect(function(plr)
	for i, v in pairs(script.Parent.Frame.ScrollingFrame:GetChildren()) do
		if v.Name == plr.Name then
			v:Remove()
		end
	end
end)

How would I make it so that the frame.wins.Text would display each player’s leaderstat value and keep updating? I’ve tried using a while true loop but it messed up the other parts.

3 Likes

What I would use is “GetPropertyChangedSignal” to detect when the value changes so you can update everything from there without having to constantly check in a loop when it changes.

1 Like
local CoreGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local FrameToBeCloned = Replicated_Storage.Template
local ScrollingFrame = script.Parent.Frame.ScrollingFrame

CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local Wins = leaderstats:WaitForChild("wins")
	
	local Frame = FrameToBeCloned:Clone()
	Frame.Name = plr.Name
	Frame.name.Text = plr.Name
	Frame.Visible = true
	Frame.Parent = ScrollingFrame
	
	
	-- I assume you got a textlabel in frame:
	Frame.TextLabel.Text = Wins.Value
	Wins.Changed:Connect(function()
		Frame.TextLabel.Text = Wins.Value
	end)
end)


Players.PlayerRemoving:Connect(function(plr)
	for _, Frame in ipairs(ScrollingFrame:GetChildren()) do
		if Frame.Name == plr.Name then
			Frame:Destroy()
		end
	end
end)
1 Like

For this, you need to listen to value changes using the .Changed event, I also fixed some other issues with your code:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)

local scroll = script.Parent.Frame:WaitForChild("ScrollingFrame")

local function PlayerAdded(player: Player)
	local leaderstats = player:FindFirstChild("leaderstats")
	local frame = game.ReplicatedStorage.Template:Clone()
	frame.wins.Text = leaderstats.wins.Value
	--this line makes it update
	leaderstats.wins.Changed:Connect(function(wins)
		frame.wins.Text = wins 
	end)
	frame.Name = player.Name
	frame.name.Text = player.Name
	frame.Visible = true
	frame.Parent = scroll
end

for _, player in pairs(game.Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end
game.Players.PlayerAdded:Connect(PlayerAdded)

game.Players.PlayerRemoving:Connect(function(player)
	local frame = scroll:FindFirstChild(player.Name)
	if frame then frame:Destroy() end 
end)

PS: Also don’t name the reference to StarterGui CoreGui. StarterGui and CoreGui are two different things, even tho StarterGui has a SetCoreGuiEnabled function.

1 Like

It works but there’s a problem.

image
image

The player that joins last will always see the full player list but the player who joins first will only see themselves.

Is your code identical to mine? Also does the console show any errors? If I’m not mistaken the following lines of code, included on the above code as well, should fix this:

for _, player in pairs(game.Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end
game.Players.PlayerAdded:Connect(PlayerAdded)

That’s the player who was already in the server before the other player was added

Change this to player:WaitForChild("leaderstats")

If this doesn’t work, then before the for _, player loop, add the following line of code:

while not game.ReplicatedStorage.Template:FindFirstChild("wins") do task.wait() end
1 Like