I made a script that’s supposed to run through every part and decal of my NPC and make it invisible. I know I can just move the NPC but its much easier to do this. Here’s my code:
local Fred = workspace:WaitForChild("FredNPC")
for _, v in pairs(Fred:GetDescendants()) do
print(v.Name)
if v:IsA("BasePart") or v:IsA("Decal") then
if v.Name ~= "HumanoidRootPart" and v.Name ~= "FacePart" then
v.Transparency = 1
print("Is now transparent!")
end
end
end
Most of the time the code works and the NPC becomes invisible, but then randomly sometimes the NPC just doesn’t become invisible and I get no errors or anything. I also feel like the output contains way too many objects for how many would actually be on the model. Can anyone help?
EDIT: I should also mention this is a localscript inside of a GUI
How could I go through every object in the model without pairs or ipairs? Also yes that’s the problem, nothing is being recognized as a basepart or decal for some reason. The code works sometimes as well which is the most confusing part. Heres an image of it working as intended:
The only code that references this character at this point is the code I provided so nothing is happening to it except loading but I have it waiting for loading (as far as I know with how :WaitForChild() works), also that screenshot is the closest I can get as this is all running on the client and there’s a fixed camera on the client, doing any action to get it closer would trigger a later function to make the NPC visible.
Is there a way to make sure the entire model is loaded before running the for loop? I thought the method was using :WaitForChild() but that appears to not be working.
just noticed the first one prints hrp like 5 times, not sure but I think there’s an event for loading the entire model, characterappearanceloaded but for models probably
Try printing all the contents of a the model without trying to change the Transparency of anything
local Fred = workspace:WaitForChild("FredNPC")
for _, v in pairs(Fred:GetDescendants()) do
print(v.Name)
end
If it’s still printing Null for some objects then theres something going on between the time your getting the model and then looping through it, also I’m pretty sure it should be returning nil not null; null isnt even in luau when something doesnt exist
local Fred = workspace:WaitForChild("FredNPC")
for _, v in pairs(Fred:GetDescendants()) do
print(v.Name)
if v:IsA("BasePart") or v:IsA("Decal") then
if v.Name ~= "HumanoidRootPart" and v.Name ~= "FacePart" then
while (v.Transparency >= 0) do
v.Transparency = 1
task.wait()
print("Is now transparent!")
end
end
end
end