Making NPC Invisible

  1. What do you want to achieve? Where at a distance, the npc will become invisible and create a particle for a brief moment

  2. What is the issue? The script apparently is not running at a certain part?

  3. What solutions have you tried so far? I tried using magnitude, and a few tutorialsā€¦ none of which worked for my situation.

So here is my code and hierarchy (sorry if the code is messy :sweat_smile:)

local Root = script.Parent:WaitForChild("HumanoidRootPart")
local Human = script.Parent:WaitForChild("Humanoid")
local Sabbath = script.Parent


while wait() do
	local e = game.Players:GetChildren()
	for i, v in pairs(e) do
		if v.Character then
			if v.Character:FindFirstChild("HumanoidRootPart") then
				if (v.Character.HumanoidRootPart.Position - Root.Position).magnitude<50 then
					Human:MoveTo(v.Character.HumanoidRootPart.Position)
					Root.Unsummon.Enabled = false
				end
			if(v.Character.HumanoidRootPart.Position - Root.Position).magnitude>50 then
				Root.Unsummon.Enabled = true
				Sabbath:GetChildren().Transparency = 1
			end
			end
		end
		wait()
	end
end

--[[local function Disappear()
	local dist = 20
	local target = nil
	for i,v in pairs (game.Workspace:GetChildren())do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("HumanoidRootPart")
		if human and torso and v ~= Sabbath then
			if (Root.Position - torso.Position).magnitude > dist then
				dist = (Root.Position - torso.Position).magnitude
				target = torso
				print("Out Of Range")
			end
		end
	end
	return target
end

while wait()do
	local torso = Disappear()
	if torso then
		print("OutOfRange")
	end
end]]--

function Attack()
	local Arrow = Instance.new("MeshPart")
	Arrow.CFrame = Sabbath.Head.CFrame
	Arrow.Size = Arrow.Size + Vector3.new(0.85, 0.3, 0.27)
	Arrow.Orientation = Arrow.Orientation + Vector3.new(0, 90, 0)
	Arrow.Parent = Sabbath.Head
	
	local weld = Instance.new("Weld")
	weld.Part0 = Sabbath.Head
	weld.Part1 = Arrow
	weld.Parent = Arrow
	weld.C0 = Sabbath.Head
	weld.C1 = Arrow
	
end

Screen Shot 2020-04-16 at 9.45.57 PM

1 Like

First thingā€™s first, the Instance:GetChildren() method returns an array containing all the children of the Instance. Trying to index Transparency which is a property, would not work. Second, everything you have below the while loop would not run, since while loops yield the thread. To solve this either use spawn or RunService.Heartbeat event. I improved your over all code and added a function to toggle the NPC visibility.

local Root = script.Parent:WaitForChild("HumanoidRootPart")
local Human = script.Parent:WaitForChild("Humanoid")
local Sabbath = script.Parent

local function ToggleVisibility(bool)
	for _,v in pairs(Sabbath:GetChildren()) do
		if v:IsA("BasePart") then
			v.Transparency = bool == true and 0 or 1
		end
	end
end

spawn(function()
	while wait() do
		local e = game.Players:GetChildren()
		for i, v in pairs(e) do
			if v.Character then
				if v.Character:FindFirstChild("HumanoidRootPart") then
					if (v.Character.HumanoidRootPart.Position - Root.Position).Magnitude < 50 then
						ToggleVisibility(true)
						Human:MoveTo(v.Character.HumanoidRootPart.Position)
						Root.Unsummon.Enabled = false
					else
						Root.Unsummon.Enabled = true
						ToggleVisibility(false)
					end
				end
			end
		end
	end
end)
3 Likes

How would I account it for making the particle last for a little?

Just add a wait(n) before disabling the particle. Make n 1 or 2 seconds. Depends on what you want.

Itā€™s a particle that will flash when he becomes invisible