Hey everyone this is a ranking system, it checks for the top 10 in the rankings and it displays their picture & name, their ranking value & it displays their team image behind their picture. Works but I'm sure it's extremely inefficient :( Could anyone improve this?
01 | Rankings = game.StarterGui.UI.RankingsMenu |
02 | repeat wait() until _G.Overall:GetSortedAsync( false , 10 ) ~ = nil |
03 | local pages = _G.Overall:GetSortedAsync( false , 10 ) |
04 | local currentPageNumber = 0 |
05 |
06 | while wait( 0.1 ) do |
07 | local data = pages:GetCurrentPage() |
08 | for _, pair in ipairs (data) do |
09 | if Rankings.R 1. TextLabel.Text = = "PLAYER" then |
10 | username = game.Players:GetNameFromUserIdAsync(pair.key) |
11 | Rankings.R 1. Overall.Text = pair.value .. " OVR" |
12 | Rankings.R 1. TextLabel.Text = string.upper(username) |
13 | Rankings.R 1. Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x = 150 &y = 200 &Format = Png&username = " .. username |
14 | if _G.Team:GetAsync(pair.key) [ 4 ] ~ = nil and _G.Team:GetAsync(pair.key) [ 4 ] ~ = 0 then |
15 | Rankings.R 1. Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key) [ 4 ] |
Programmers are lazy. We never want to write more than we have too, and therefore try to never write the same thing twice.
You are doing the exact same thing for each individual Ranking (1-10) except with different values. This is where a for loop would come in. You want to check if each GUI's (R1-R10) TextLabel's text is equal to "PLAYER", so therefore we could use a numeric for loop and just concatenate the current iteration onto an "R" to get which GUI we are currently checking.
We can than just use the code you used on each if statement, and simply break if the condition passes (so it doesn't continue checking).
01 | for rank = 1 , 10 do |
02 | local ranking = Rankings [ "R" ..rank ] -- get current GUI to check |
03 | if ranking.TextLabel.Text = = "PLAYER" then |
04 | username = game.Players:GetNameFromUserIdAsync(pair.key) |
05 | ranking.Overall.Text = pair.value .. " OVR" |
06 | ranking.TextLabel.Text = string.upper(username) |
07 | ranking.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x = 150 &y = 200 &Format = Png&username = " .. username |
08 | if _G.Team:GetAsync(pair.key) [ 4 ] ~ = nil and _G.Team:GetAsync(pair.key) [ 4 ] ~ = 0 then |
09 | ranking.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key) [ 4 ] |
10 | end |
11 | break -- Stop checking if condition passes |
12 | end |
13 | end |
Note this is just a snippet to simplify your cascading if statement.