Hello, I am making a script where when the event is fired the decal / any imageobject inside a part changes its image id, I thought this would be simple, but
here are some decals in a folder. They have a image id attribute inside of them (their texture) like any other imageobject.
When this runs it searches the decal values folder for a decal named the ‘fraction’ variable’s value. after that It should take the image id and put it into every decal inside of the target part. Problem being that when it runs the decals only changes to one of the possible textures and even when the fraction variable changes it doesn’t affect it.
local children = target:GetChildren()
for i = 1, #children do
local inst:Instance = children[i]
if not inst then print("Couldn't find instance") return end
if inst:IsA("Decal") then
local decal:Decal = children[i]
if not decal then print("Couldn't find decal") return end
local fraction = Health / maxhealth * 100
print(fraction)
for j = 1, 9 do
if fraction > (j * 10) then
local decalvalue = decalvalues:FindFirstChild(tostring(j * 10))
if not decalvalue then print("Couldn't find decalvalue ") return end
decal.Texture= decalvalues:FindFirstChild(j*10).Texture
break
end
end
end
end
what could be the problem, I am also not sure about is it the loops or what?
Also yes I have checked that the decals folder’s decals textures are valid
I’m totally guessing this, as I have no clue what could be truly wrong.
Is Health and maxhealth a decimal going from 0 to 1, or integers from 0 to 100? How do they work? My only guess is that your fraction computation is not following (PE)MDAS?
-- If Health and maxhealth are both decimals going from 0 to 100...
local fraction = (Health / maxhealth) * 100
-- If Health goes from 0 to 100, but maxhealth is 0 to 1...
local fraction = Health / (maxhealth * 100)
Additional note about the way you handle the decal checking thing, it’s better to utilize the math functions, such as math.floor.
The following example assumes the Health and maxhealth variables can reach 100.
local fraction = math.floor((Health / maxhealth) * 10) * 10 -- No decimals.
-- Eliminates the for loop.
local decalvalue = decalvalues:FindFirstChild(tostring(fraction)
if not decalvalue then print("Couldn't find decalvalue ") return end
decal.Texture= decalvalues:FindFirstChild(j*10).Texture
-- "break" isn't needed anymore as the for loop was removed.
The target variable is part that has decals inside of them (one decal for each side) .
I am trying to make it so the script should look through the folder of decals for the one with the name matching the fraction, the fraction can be anywhere from 0 to 100 depending on the health of the target and then after getting the matching or closest one it takes that decal’s texture id and puts it into the target’s decals. I put >= so if the folder doesn’t have a decal exactly named after the fraction it can look for the next closest to the name
Hey , I don’t know how but suddently the decal textures are in order and working, I guess they work fine for now , I took the for loop out and its fine for now