Help making UI image change depending on group rank

Hello,

I am trying to make it so depending on your rank in the group, the startergui image changes locally.

My problem is that the image id is not being changed and just remains as the placeholder. I have tried deleting unnecessary variables and adding concatenations, but I’m not sure what else could be a potential issue.

All of the images used are stored in asset manager (if it means anything), my script is inside of its label inside of startergui.

Here is my code:

local rank = game.Players.LocalPlayer:GetRankInGroup(4939664)
local image = script.Parent.Image

if rank == 1 then
	image = "rbxassetid://12340415890"
elseif rank == 2 then
	image = "rbxassetid://12340415890"
elseif rank == 3 then
	image = "rbxassetid://12340416075"
elseif rank == 4 then
	image = "rbxassetid://12340416205"
elseif rank == 5 then
	image = "rbxassetid://12340416332"
elseif rank == 6 then	
	image = "rbxassetid://12340416462"
elseif rank == 7 then
	image = "rbxassetid://12340416628"
elseif rank == 8 then
	image = "rbxassetid://12340416826"
elseif rank == 9 then
	image = "rbxassetid://12340417013"
elseif rank == 10 then
	image = "rbxassetid://12340417265"
elseif rank == 11 then
	image = "rbxassetid://12340417520"
elseif rank == 12 then
	image = "rbxassetid://12340417770"
elseif rank == 13 then
	image = "rbxassetid://12340417999"
elseif rank == 14 then
	image = "rbxassetid://12340418183"
elseif rank == 15 then
	image = "rbxassetid://12340418491"
elseif rank == 16 then
	image = "rbxassetid://12340418633"
elseif rank == 17 then
	image = "rbxassetid://12340418804"
elseif rank == 18 then
	image = "rbxassetid://12340418970"
elseif rank == 19 then
	image = "rbxassetid://12340419145"
elseif rank >= 20 then
	image = "rbxassetid://12340419410"	
else
	image = "rbxassetid://12340415890"
end

Thank you!

Why the hell are you using loads of elseif 's? It would be way better and readable if you where to just create a dictionary with all of the ranks inside and then just searching for the rank inside of the dictionary.

Could you explain what your issue is? You have said it is logic error but this could mean anything?

2 Likes

Couldn’t you just do something like this?

for i = 1, 20 do
	if rank == i then
		-- 
	end
end
1 Like

I didn’t know what else to use other than elseif. My issue is that my image Id is not being changed and just remains as the initial placeholder, sorry I wasn’t very clear in the message.

Thanks.

I see what you mean and that my code isn’t efficient, but if I knew how to use anything aside from a list of elseifs, I would.

With the code you just suggested I use, I wouldn’t know how to put it all together.

Thank you.

Alright, I will come back to you once I have organized your code a bit. Then if the code doesn’t work after organization we will try fix it.

1 Like

I really appreciate that, thank you!

After doing some testing I think I know the issue. If rather then doing .Image right away in the variable, you should set the image inside of the if statement (or however you change it up to make the code better).

1 Like
local rankImages = {
	[1] = "rbxassetid://12340415890",
	[2] = "rbxassetid://12340415890", 
	[3] = "rbxassetid://12340416075",
	[4] = "rbxassetid://12340416205",
	[5] = "rbxassetid://12340416332",
	[6] = "rbxassetid://12340416462",
	[7] = "rbxassetid://12340416628",
	[8] = "rbxassetid://12340416826",
	[9] = "rbxassetid://12340417013",
	[10] = "rbxassetid://12340417265",
	[11] = "rbxassetid://12340417520",
	[12] = "rbxassetid://12340417770",
	[13] = "rbxassetid://12340417999",
	[14] = "rbxassetid://12340418183",
	[15] = "rbxassetid://12340418491",
	[16] = "rbxassetid://12340418633",
	[17] = "rbxassetid://12340418804",
	[18] = "rbxassetid://12340418970",
	[19] = "rbxassetid://12340419145",
	[20] = "rbxassetid://12340419410",
	
	["Default"] = "rbxassetid://12340415890"	
}

local rank = game.Players.LocalPlayer:GetRankInGroup(4939664)
local image = script.Parent.Image

for i = 1, 19 do
	if rank == i then
		image.Image = rankImages[i]
	end
end

-- exceptions

if rank >= 20 then
	image.Image = rankImages[20]
end

if rank < 1 then
	image.Image = rankImages["Default"]
end

I think this should work but I haven’t tested it. If you need me to explain anything in the script, please go ahead!

1 Like

Why would you even do a loop? Could you not just do an if statement checking if the rank is inside of the dictionary via an if statement and if it is then change it. Although the loop would work it would just be more simple to do it via like 1-2 lines of code.

1 Like

This script works perfectly, thank you.

But I just also wanted to point out that in local image = script.Parent.Image you didn’t have to put .image at the end because that would have caused the script to fail.

1 Like

Yeah, to be fair that does seem easier, hadn’t thought of that. I appreciate the tip.

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