u/Healthy_Arrival872

I made a leaderboard script and put it through chatgpt to see what it would improve

human:

leaderboard = game.Workspace.Part.SurfaceGui.Frame

template = leaderboard.Template

playeronleaderboard = 0

function removeplayderfromleaderboard(player)

for i,v in pairs(leaderboard:GetChildren()) do

	if v:IsA("TextLabel") then

		if v.Name == player.Name then

v:Destroy()

		end

	end

end

end

game.Players.PlayerAdded:Connect(function(player)

local playerleaderboardpos = playeronleaderboard + 1

local playerleaderstats = Instance.new("Folder")

[playerleaderstats.Name](http://playerleaderstats.Name) = "leaderstats"

playerleaderstats.Parent = player

local Money = Instance.new("IntValue")

[Money.Name](http://Money.Name) = "Money"

Money.Parent = player.leaderstats

playerleaderstats.Money.Value = 1000



local leaderboardpos = player.leaderstats.Money.Value

local newplayer = template:Clone()

newplayer.Name = player.Name

newplayer.Text = [player.Name](http://player.Name) .. " " .. leaderboardpos

newplayer.Visible = true

newplayer.Parent = leaderboard

playeronleaderboard = playeronleaderboard + 1

player.leaderstats.Money.Changed:Connect(function()

	newplayer.Text = [player.Name](http://player.Name) .. " " .. player.leaderstats.Money.Value

end)

if playeronleaderboard > 1 then

	newplayer.Position = UDim2.new(0, 0, 0, (playerleaderboardpos - 1) \* 70)

end

end)

game.Players.PlayerRemoving:Connect(function(player)

removeplayderfromleaderboard(player)

playeronleaderboard = playeronleaderboard - 1

end)

ai refined:

local DataStoreService = game:GetService("DataStoreService")

local Players = game:GetService("Players")

local MoneyStore = DataStoreService:GetDataStore("MoneyStore")

local leaderboard = workspace.Part.SurfaceGui.Frame

local template = leaderboard.Template

local allPlayersData = {}

-- CREATE LEADERBOARD UI

local function updateGlobalLeaderboard()

\-- REMOVE OLD LABELS

for \_,v in pairs(leaderboard:GetChildren()) do

	if v:IsA("TextLabel") and [v.Name](http://v.Name) \~= "Template" then

		v:Destroy()

	end

end



\-- SORT PLAYERS

local sorted = {}



for userId,data in pairs(allPlayersData) do

	table.insert(sorted,data)

end



table.sort(sorted,function(a,b)

	return a.Money > b.Money

end)



\-- CREATE NEW LABELS

for i,data in ipairs(sorted) do



	if i > 25 then

		break

	end



	local newPlayer = template:Clone()



	newPlayer.Visible = true

	newPlayer.Name = data.Name



	newPlayer.Text =

		"#"..i..

		" | "..data.Name..

		" | $"..data.Money



	newPlayer.Position = UDim2.new(0,0,0,(i-1)\*50)



	newPlayer.Parent = leaderboard

end

end

-- LOAD PLAYER

Players.PlayerAdded:Connect(function(player)

\-- LEADERSTATS

local leaderstats = Instance.new("Folder")

[leaderstats.Name](http://leaderstats.Name) = "leaderstats"

leaderstats.Parent = player



local Money = Instance.new("IntValue")

[Money.Name](http://Money.Name) = "Money"

Money.Parent = leaderstats



\-- LOAD DATA

local success,data = pcall(function()

	return MoneyStore:GetAsync(player.UserId)

end)



if success and data then

	Money.Value = data

else

	Money.Value = 1000

end



\-- UPDATE TABLE

allPlayersData\[player.UserId\] = {

	Name = [player.Name](http://player.Name),

	Money = Money.Value

}



updateGlobalLeaderboard()



\-- MONEY CHANGED

Money.Changed:Connect(function()



	allPlayersData\[player.UserId\] = {

		Name = [player.Name](http://player.Name),

		Money = Money.Value

	}



end)

end)

-- SAVE PLAYER

Players.PlayerRemoving:Connect(function(player)

local money = player.leaderstats.Money.Value



pcall(function()

	MoneyStore:SetAsync(player.UserId,money)

end)



allPlayersData\[player.UserId\] = {

	Name = [player.Name](http://player.Name),

	Money = money

}



updateGlobalLeaderboard()

end)

-- AUTOSAVE + REFRESH EVERY MINUTE

while true do

task.wait(60)



\-- SAVE ONLINE PLAYERS

for \_,player in pairs(Players:GetPlayers()) do



	local money = player.leaderstats.Money.Value



	pcall(function()

		MoneyStore:SetAsync(player.UserId,money)

	end)



	allPlayersData\[player.UserId\] = {

		Name = [player.Name](http://player.Name),

		Money = money

	}

end



\-- REFRESH LEADERBOARD

updateGlobalLeaderboard()

end

note: both server their purpose and have no issues, although the AI messed up on the leaderboard positioning and made it times 50 instead of times 80 which made them overlap but it was just a change off a single value and it was fixed. Also note that I have no idea how datastore works and didnt verify if it works.

reddit.com
u/Healthy_Arrival872 — 8 days ago