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
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
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 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)
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
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
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?