Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I compact this function (preferably using operators) and make it more efficient?

There's no way this function is as compact as I can get it to be. Would anyone be able to help me learn how to do this process more efficiently?

local function getRank(plr)
	local lvl = plr.leaderstats.Level.Value
	local r = Instance.new("StringValue", plr:WaitForChild("leaderstats"))
	if lvl < 10 then
		r.Value = "Intern Elf"
	elseif lvl >= 10 and lvl < 20 then
		r.Value = "Junior Elf"
	elseif lvl >= 20 and lvl < 30 then
		r.Value = "Apprentice Elf"
	elseif lvl >=30 and lvl < 40 then
		r.Value = "Professional Elf"
	elseif lvl >= 40 and lvl < 50 then
		r.Value = "Experienced Elf"
	elseif lvl >= 50 and lvl < 60 then
		r.Value = "Senior Elf"
	elseif lvl >= 60 and lvl < 70 then
		r.Value = "Supervisor Elf"
	elseif lvl >= 70 and lvl < 80 then
		r.Value = "Master Elf"
	elseif lvl >= 80 and lvl < 90 then
		r.Value = "Grand Master Elf"
	elseif lvl >= 90 then
		r.Value = "Santa's Assistant"
	end
end

1 comment

0

Do you really need to compact it more?

2 answers

✓ Accepted answer
local ElfTypes = {

    "Intern Elf",
    "Junior Elf",
    "Apprentice Elf",
    "Professional Elf",
    "Experienced Elf",
    "Senior Elf",
    "Supervisor Elf",
    "Master Elf",
    "Grandmaster Elf",
    "Santa's Assistant";

}


local function SetElfTypeFromLevel(Player)
    local Leaderstats = Player.leadertats
    ---------------
    local ElfType = Leaderstats.ElfType
    local Level = Leaderstats.Level
    ---------------
    for LevelIndex, Type in pairs(ElfTypes) do
        ---------------
        local LevelMargin = LevelIndex * 10
        ---------------
        if (Level.Value >= LevelMargin and Level.Value < LevelMargin) then
            ---------------
            ElfType.Value = Type
        end
    end
end

1 comment

0

chef's kiss

What you could do is make a table that has tables that include the rank and then the minimum level required for it, like so:

--Put this at the top of the function
local ranks = {{"Intern Elf", 0}, {"Junior Elf", 10}}

Then after that, let's go ahead and get the rank and create the value:

local playerLevel = plr.Leaderstats.Level.Value
--We don't need to use WaitForChild, as we know the leaderstats already exists
local rank = Instance.new("StringValue", plr.Leaderstats)

Then, we loop through everything in the table and check if the player's level is within the required parameters:

--Loop through the ranks table
for i, rankInfo in pairs (ranks) do
	--Set the values into variables to make the code look cleaner
	local rankName = rankInfo[1]
	local rankMinimumLevel = rankInfo[2]

	--Check if the player's level is within the level threshold
	if playerLevel >= rankMinimumLevel and playerLevel < (rankMinimumLevel + 10) then
		--Give them the rank!
		rank.Value = rankName
	end
end

Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.