Code to make an NPC invisible doesn't work half the time

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

image

I believe it’s better to do a for loop without pairs or ipairs, also I can’t see it printing your second print check.

1 Like

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:
image

Is there something happening to the character that might make things inside of it return Null?

Also that screenshot is way too small

1 Like

first:

for i, v in e:GetChildren()

second:
are you running it on different models each time? could be the issue

1 Like

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.

Nope I’m only making a single model invisible at this time, I’ll try that code a couple times and see if it works!

They need to loop with GetDescendants so they can get the Decal of the face

my point was using in vs in pairs

1 Like

First two times I tried it it didnt work but the third time it did?

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.

That’s exactly what WaitForChild is for

1 Like

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

1 Like

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

1 Like

are you using free models? as crusader said, null doesn’t appear in luau to my knowledge

2 Likes

APPOLOGIES!! The Null comes from animation saves as I use moon animator.

image

1 Like

try this

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
1 Like

Also heres the output you asked for earlier I think the animation saves might be why theres so many duplicates of things:




Just try this real quick and tell me what happens

1 Like

It forever loops