More efficient way of multiple elseif statements

I was wondering if there’s a healthier/more efficient method for this big block of elseif’s that will hand out identical output.

This looks really bad and I’ve been trying to find alternatives but faild to do so.

The point of the script is to check which rank the player has in a specific group, and change the variables rankText and rankColor to then use them in changing a headtitle.

This is a 1:1 replica of the code but changed to hide it’s original contents:

		local Rank = player:GetRoleInGroup(1)
		
		if Rank == "Specific Rank1" then
			rankText = "Specific Rank1"
			rankColor = Color3.fromRGB(255, 255, 255)
		elseif Rank == "Officer" then
			rankText = "Officer"
			rankColor = Color3.fromRGB(254, 254, 254)
		elseif Rank == "Sergeant" then
			rankText = "Sergeant"
			rankColor = Color3.fromRGB(253, 253, 253)
		end
1 Like

Use a dictionary.

local Table = {
     ['Specific Rank1'] = Color3.fromRGB(255, 255, 255),
     ['Officer'] = Color3.fromRGB(254, 254, 254),
     ['Sergeant'] = Color3.fromRGB(253, 253, 253),
}
3 Likes

should look something like this

local Table = {
     ['Specific Rank1'] = Color3.fromRGB(255, 255, 255),
     ['Officer'] = Color3.fromRGB(254, 254, 254),
     ['Sergeant'] = Color3.fromRGB(253, 253, 253),
}

local Rank = player:GetRoleInGroup(1)
		
rankText = Rank
rankColor = Table[Rank]
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.