Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can this script be made more efficient?

Asked by 9 years ago

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?

01Rankings = game.StarterGui.UI.RankingsMenu
02repeat wait() until _G.Overall:GetSortedAsync(false,10) ~= nil
03local pages = _G.Overall:GetSortedAsync(false, 10)
04local currentPageNumber = 0
05 
06while wait(0.1) do 
07local data = pages:GetCurrentPage()
08for _, pair in ipairs(data) do
09if Rankings.R1.TextLabel.Text == "PLAYER" then 
10username = game.Players:GetNameFromUserIdAsync(pair.key)
11Rankings.R1.Overall.Text = pair.value .. " OVR"
12Rankings.R1.TextLabel.Text = string.upper(username)
13Rankings.R1.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
14if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
15Rankings.R1.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
View all 97 lines...
0
I highly doubt anyone is going to read all of it and then post you a more efficient answer if it already works.. Uroxus 350 — 9y
0
my gosh this hurts my eyes. please tab correctly. Perci1 4988 — 9y

1 answer

Log in to vote
3
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

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

01for 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
13end

Note this is just a snippet to simplify your cascading if statement.

0
Thank you very much for the explanation! jordan0810 55 — 9y
0
No problem :) BlackJPI 2658 — 9y
Ad

Answer this question