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

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

Does it change things to be a transparency of 1 though? (invisible)

1 Like

even if it did 1 is still greater or equal to 0

2 Likes

It makes one part invisible (the glasses) but nothing else is.
image

1 Like

Made it while (v.Transparency < 1) do and now it works! Thank you both so much!! Roblox is really weird with loading orders and things sometimes.

2 Likes

Dang yeah I guess i got confused with the sign and which way it goes, glad you got it working!

1 Like

Hmm, I would not recommend because it uses a bit of performance. Personally, I would add this in the beginning of your script :

if not game:IsLoaded() then
    game.Loaded:Wait()
end

But it’s you choice

The problem still happens even when I do that, I’m very unsure why but it does.

I think it’s a problem of replication of parts from the server to the client…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.