What am I doing wrong here [decals]

I am trying to make it so depending on what the fraction is it chooses a decal from the table, I have checked that all of the pictures are correct and that the fraction is correct, but when it changes the decals in the loop it gives the wrong textures.

for _, v2 in pairs(target:GetChildren()) do
				  

				if v2:IsA("Decal") then 
local Fraction=  Health / maxhealth * 100

					--the pictures depending on how big procentage the current health is from the max
					--I believe there would be a better way of doing this table since I don't need 10 different pictures I only need a few.

					local PossiblePictures = {
						
						[10] = decalvalues:FindFirstChild("10").Texture,
						[20] =  decalvalues:FindFirstChild("20").Texture,
						[30] =  decalvalues:FindFirstChild("30").Texture,
						[40] =  decalvalues:FindFirstChild("40").Texture,
						[50] =  decalvalues:FindFirstChild("50").Texture,
						[60] =  decalvalues:FindFirstChild("60").Texture,
						[70] =  decalvalues:FindFirstChild("70").Texture,
						[80] =  decalvalues:FindFirstChild("80").Texture,
						[90] =  decalvalues:FindFirstChild("90").Texture,
						
					}
					print(Fraction)
					for BlockHealth, Texture in pairs(PossiblePictures) do
						
						if Fraction >= BlockHealth then
							
							v2.Texture = Texture
						else
							break
						end
					



				

		end
end

Yeah, I don’t think that really works

that does not work either , I don’t think I need new loops,

What’s the error message in output

there is no “Error message”, I tried your sample code but it just didn’t work for changing the decal, I there a possibility that my example code could work, beacause It for some reason doesn’t

could you maybe help me with my example code, if possible?

Without more information of your data architecture
I wouldn’t really be able to help

Such of, what is ‘target:GetChildren()’
For example, you don’t want to be creating a table of 9 different values for each iteration of target:GetChildren(). If you have 20 children in target, than you’re doing 20 allocations for (20 * (9 * 64) total memory (11520 bytes, or 11.5Kb) of memory for this one iteration, which the garbage collector doesn’t immediately dispose

Input my code and tell me the error message
It should work. I can’t really say beyond that though because don’t have enough information and no error messages to determine
There’s now three possible error messages;
Target has no children.
There’s no decal instances of the children
Or in decalvalues there’s not a value object specified by your would-be table index

I mean it could work, but The variables in the example of yours make it seem like I should replace Everything in the original code ? Because in one of the examples you used “v2” variable , but was I supposed to keep the v2 loop in there?

Oh yeah that v2 was leftover from the pairs() iteration my mistake, should’ve been something to do with the for-iterations, btw, they’re faster in lua, so should always use them. I mean, there should’ve been an error message for v2 doesn’t exist, however you stated there’s not any error messages, so it makes me wonder if the iteration occured at all and something before the iteration is throwing an exception. I don’t have enough information to determine that. Maybe it doesn’t display a message when a nonexistent variable is used in iteration, I don’t know.

okay , I will test around with that code and see if it works, thanks for the help anyways.

okay , I have seem to find an issue, it does not get the right texture out of the decals stored in the decalvalues same problem as in the first piece of code.
I checked , there is nothing wrong with the fraction , but the decal texture doesn’t change even tho every decal in the folder has a different texture (yes those textures are real)
NÀyttökuva 2022-08-29 211353

What is decalvalues:FindFirstChild() is that a local variable?
I note your folder is named DecalValues not decalvalues, unless decalvalues is a local variable.

If it’s not that, then makes me wonder if it’s something to do with how decals work.
https://developer.roblox.com/en-us/api-reference/property/Decal/Texture
and according to the documentation on changing a texture the Id isn’t being directly used. I’ve never worked with textures so I can’t say for certain if there’s an issue. Not sure if you can just assign a .Texture with another .Texture directly

Actually, maybe, decal.Texture = decalvalues:FindFirstChild() would work

1 Like

I mean the thing is the first time the texture is changed to the texture in the folder it works , but when ran second time it doesn’t change the decals inside the target to the desired texture

If you believe its because of decals, I can try surface gui, and see what happens

It can’t be because of only decals, I tried ‘Surfacegui’ and a imagelabel, but the image id just does not change the second time

Have you double checked to see if the textures are all labelled correctly?

Also, try a print statement of the values Fraction and BlockHealth before the if Fraction >= Blockhealth line to see what the numbers are.

It’s possible that floating point errors in calculations are giving values like .99999 instead of 1.0000 so that trips up the >= part if it ends up being false.

Another way would be just to put all the Textures in the Part as Transparent (Transparency = 1), then just make the one you need opaque (Transparency = 0). Just make sure to reset the other ones Transparent so you don’t get more than one showing up at a time.

Also, this script only runs once. Is it just part of a script that makes it run multiple times?