Simplify code (help)

How would i simplify this since it takes too much space and i believe is way too unnecessary.
This is for changing progress bar on detection in a stealth game.

local container = menustore[npc].ContainerContainer.Container
			if currentlyDetecting[npc]/100 == 0 then
				container.fullgray.Visible = true
			elseif currentlyDetecting[npc]/100 == 1 then
				container.detecting1.Visible = true
			elseif currentlyDetecting[npc]/100 == 2 then
				container.detecting2.Visible = true
			elseif currentlyDetecting[npc]/100 == 3 then
				container.detecting3.Visible = true
			elseif currentlyDetecting[npc]/100 == 4 then
				container.detecting4.Visible = true
			elseif currentlyDetecting[npc]/100 == 5 then
				container.detecting5.Visible = true
			elseif currentlyDetecting[npc]/100 == 6 then
				container.detecting6.Visible = true
			elseif currentlyDetecting[npc]/100 == 7 then
				container.detecting7.Visible = true
			elseif currentlyDetecting[npc]/100 == 8 then
				container.detecting8.Visible = true
			elseif currentlyDetecting[npc]/100 == 9 then
				container.detecting9.Visible = true
			elseif currentlyDetecting[npc]/100 == 10 then
				container.detecting10.Visible = true
			elseif currentlyDetecting[npc]/100 == 11 then
				container.fullred.Visible = true
		
				
			end
4 Likes

Why don’t you first divide the thing by 100 and then see if it’s bigger or equal to 0 and small or equal to 12?

Ok but what about simplifying it even further

i just realised im repeating the same thing pretend that full gray is different every time

Ok well just enable the frame that has the name the result of the /100 division

Dang you actually pulled off a Yandere Simulator spaghetti code.

It seem’s like all the elseif statements are doing the same thing so how about doing it like this

local container = menustore[npc].ContainerContainer.Container
			if currentlyDetecting[npc]/100 >= 0 and currentlyDetecting[npc]/100 <= 12 then
				container.fullgray.visible = true
			end
1 Like

sorry i forgot to change the code, the elseif statment do smth different every time (changed the post)

that might actually work wait a second

If that’s the case then you should pre allocate all the variables into a table

local containerColors = {}
containerColors[1] = container.fullgray.visible;
containerColors[2] = container.red.visible;
containerColors[3] = container.(idk whatever variables that points to the color).visible;
-- continue 4 to 12 from here, you get the idea

local container = menustore[npc].ContainerContainer.Container
	local temp = currentlyDetecting[npc]/100;
	if temp >= 0 and temp <= 12 then
		containerColors[temp] = true;
	end

Btw I’m not sure if currentlyDetecting[npc]/100 is guaranteed to be within 0 to 12, but if it is then you can just remove the if statement completely so that it’s faster.

You can math.clamp it if it’s suited

what is math.clamp. I know its a math function but what does it do

math.clamp simply forces the number to be within a certain range, eg if your number is 100 but you clamped it to 0-50 then it will change to 50, same if its lesser than 0.

But if your number is within that 0-50 range for example 13 then it will keep that number 13

also if i wanted to for example speed up the rates of detection, how would i go about doing that

I’m not sure what detection you mean by that and if this question is a separate problem from the thread you created then you should create another thread since it’s not really related to the current one right now. This should help other people identify the problem instead of solving all of your questions in one long thread which will be harder to read.