New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How can this script be made more efficient?

Asked by 3 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?

Rankings = game.StarterGui.UI.RankingsMenu
repeat wait() until _G.Overall:GetSortedAsync(false,10) ~= nil
local pages = _G.Overall:GetSortedAsync(false, 10) 
local currentPageNumber = 0

while wait(0.1) do  
local data = pages:GetCurrentPage() 
for _, pair in ipairs(data) do 
if Rankings.R1.TextLabel.Text == "PLAYER" then  
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R1.Overall.Text = pair.value .. " OVR"
Rankings.R1.TextLabel.Text = string.upper(username)
Rankings.R1.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R1.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R2.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R2.Overall.Text = pair.value .. " OVR"
Rankings.R2.TextLabel.Text = string.upper(username)
Rankings.R2.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R2.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R3.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R3.Overall.Text = pair.value .. " OVR"
Rankings.R3.TextLabel.Text = string.upper(username)
Rankings.R3.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R3.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R4.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R4.Overall.Text = pair.value .. " OVR"
Rankings.R4.TextLabel.Text = string.upper(username)
Rankings.R4.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R4.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R5.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R5.Overall.Text = pair.value .. " OVR"
Rankings.R5.TextLabel.Text = string.upper(username)
Rankings.R5.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R5.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R6.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R6.Overall.Text = pair.value .. " OVR"
Rankings.R6.TextLabel.Text = string.upper(username)
Rankings.R6.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R6.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R7.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R7.Overall.Text = pair.value .. " OVR"
Rankings.R7.TextLabel.Text = string.upper(username)
Rankings.R7.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R7.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R8.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R8.Overall.Text = pair.value .. " OVR"
Rankings.R8.TextLabel.Text = string.upper(username)
Rankings.R8.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R8.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R9.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R9.Overall.Text = pair.value .. " OVR"
Rankings.R9.TextLabel.Text = string.upper(username)
Rankings.R9.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username 
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R9.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end
elseif Rankings.R10.TextLabel.Text == "PLAYER" then
username = game.Players:GetNameFromUserIdAsync(pair.key)
Rankings.R10.Overall.Text = pair.value .. " OVR"
Rankings.R10.TextLabel.Text = string.upper(username)
Rankings.R10.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username    
if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then
Rankings.R10.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4]
end 
end 
end
if pages.IsFinished then    
break
end
pages:AdvanceToNextPageAsync()  
currentPageNumber = currentPageNumber + 1
end
script.Parent["[Pre-Load]Leaderboards"]:Destroy()
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 — 3y
0
my gosh this hurts my eyes. please tab correctly. Perci1 4911 — 3y

1 answer

Log in to vote
3
Answered by
BlackJPI 2620 Snack Break Moderation Voter Community Moderator
3 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).

for rank = 1, 10 do
    local ranking = Rankings["R"..rank] -- get current GUI to check
    if ranking.TextLabel.Text == "PLAYER" then  
        username = game.Players:GetNameFromUserIdAsync(pair.key) 
        ranking.Overall.Text = pair.value .. " OVR" 
        ranking.TextLabel.Text = string.upper(username) 
        ranking.Player.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=150&y=200&Format=Png&username=" .. username 
        if _G.Team:GetAsync(pair.key)[4] ~= nil and _G.Team:GetAsync(pair.key)[4] ~= 0 then 
            ranking.Team.Image = "rbxassetid://" .. _G.Team:GetAsync(pair.key)[4] 
        end
        break -- Stop checking if condition passes
    end
end

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

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

Answer this question