Need Help with NPC Despawning Script

I want to make a script that depspawn an NPC after a set amount of time
the despawn process involved npc slowly turn transparent
and after all part turn completely transparented proceed to destroy the model
I know how to do tween but I don’t know where I should put the destroy funtion
also I want the countdown to only start after the model got clone into a folder in the workspace
I tried using Tween.Completed:wait() but this will make the process extremely slow since it will go through all the part in model one by one
I tried using Task.Wait(0) but that will immediatly destroy the npc after the wait end without any fade out effect i want

please help

local NPC = script.Parent
local Tween = game:GetService("TweenService")
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 1)
local TargetTransparency = 1

local function Despawning()
	wait(10)
	for _, part in pairs(NPC:GetChildren()) do
		if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" and part.Transparency ~= 1 then
			local Tween = Tween:Create(part, Info, {Transparency = TargetTransparency})
			Tween:Play()
		end
	end
end

workspace["Civilian NPCs"].ChildAdded:Connect(Despawning)

-- all of this make npc go invisible but I have no idea how to destroy the model after all the part turn transparent
1 Like
Tween.Completed:Connect(function()
    -- Destroy
end)

this will only destroy the model after one part turn transparent tho
I want the npc to get destroy after all part of its body turn transparent

Where is the script currently, is it inside an NPC or inside a folder?

it is inside the npc model
image

Here:

local NPC = script.Parent
local Tween = game:GetService(“TweenService”)
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 1)
local TargetTransparency = 1

local function Despawning()
wait(10)
for _, part in pairs(NPC:GetChildren()) do
if part:IsA(“BasePart”) and part.Name ~= “HumanoidRootPart” and part.Transparency ~= 1 then
local Tween = Tween:Create(part, Info, {Transparency = TargetTransparency})
Tween:Play()
Tween.Completed:Wait()
NPC:Destroy()
end
end
end

workspace[“Civilian NPCs”].ChildAdded:Connect(Despawning)

Nevermind, I got what you were asking for here is the script:

local Tween = game:GetService(“TweenService”)
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 1)
local TargetTransparency = 1

local function Despawning(NPC)
wait(10)
for _, part in pairs(NPC:GetChildren()) do
if part:IsA(“BasePart”) and part.Name ~= “HumanoidRootPart” and part.Transparency ~= 1 then
local Tween = Tween:Create(part, Info, {Transparency = TargetTransparency})
Tween:Play()
end
end
end

workspace[“Civilian NPCs”].ChildAdded:Connect(function()
Despawning(script.Parent)
task.wait(3.5)
script.Parent:Destroy()
end)

After the tween completes the npc gets deleted, have fun making your game and let me know if it works.

[EDIT] I am so sorry i made a mistake i left some code from my main game script i just noticed sorry now everything should be alright😅

I’m assuming the MarketPlace and GamepassID is from your game
so I modify your script a bit and it still the same
the model immeditaly got deleted after the wait end

Try this:

local Tween = game:GetService("TweenService")
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 1)
local TargetTransparency = 1

local function Despawning(NPC)
	wait(10)
	for _, part in pairs(NPC:GetChildren()) do
		if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" and part.Transparency ~= 1 then
			local Tween = Tween:Create(part, Info, {Transparency = TargetTransparency})
			Tween:Play()
		end
	end
end

workspace["Civilian NPCs"].ChildAdded:Connect(function()
	Despawning(script.Parent)
	task.wait(3.5)
	script.Parent:Destroy()
end)

i have done it again, please add a boolvalue to the NPC model called "IsInDeletionProgress " then paste this script:

local Tween = game:GetService(“TweenService”)
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 1)
local TargetTransparency = 1

local IsInDeletionProgress = script.Parent.IsInDeletionProgress.Value

local function Despawning(NPC)
wait(1)
for _, part in pairs(NPC:GetChildren()) do
if part:IsA(“BasePart”) and part.Name ~= “HumanoidRootPart” and part.Transparency ~= 1 then
local Tween = Tween:Create(part, Info, {Transparency = TargetTransparency})
Tween:Play()
end
end
end

workspace[“Civilian NPCs”].ChildAdded:Connect(function()
if IsInDeletionProgress == false then
script.Parent.IsInDeletionProgress.Value = true
Despawning(script.Parent)
task.wait(5.1)
script.Parent:Destroy()
else
print(“ThisNpcIsBeingDeleted”)
end

end)

This should work as intended

1 Like

Ok this one seem to working, the npc spawn and fade away before deleted
but, they become a ghost

what I meant:

It might have something to do with the way I add npc
since I constantly spawning npc into the folder the funtion probably override each other and result in the model having no time to tween out before getting delete

The way I spawn NPC:

is it better if I put the fade in script inside the model instead??
I kinda didn’t think ahead after I done something

Only mark it as the solution if it 100% works, so other people can come to help.

I’m currently debugging it and seeing what is going wrong.

Yes, the script i provided should work if put inside the NPC model that you seem to have in ReplicatedStorage.
How does you script spawn NPCs?
image

local RPS = game:GetService("ReplicatedStorage")
local RandomColor = script.Parent.RandomColor
local NPC = RPS.Civilian

local Folder = script.Parent
local MaxNPC = 53
local SpawnLocation = workspace["Path Checkpoints"]:GetChildren()

function SpawnNPC()
	local Clone = NPC:Clone()
	local RandomPosition = SpawnLocation[math.random(1, #SpawnLocation)]
	if RandomColor.Value == true then
		local RandomTorsoColor = BrickColor.Random()
		Clone.Torso.BrickColor = RandomTorsoColor
		Clone.Torso.Light.Color = RandomTorsoColor.Color
	end
	Clone:SetPrimaryPartCFrame(RandomPosition.CFrame)
	Clone.Parent = Folder
end

while task.wait(1) do
	if #Folder:GetChildren() < MaxNPC then
		SpawnNPC()
	end
end

The script is fine, it should work if you put the script in the npc :+1:

I got the npc fading to work, but if you want the accessories to become transparent, give me a minute

Here is the code:

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local TargetTransparency = 1

local function Despawning()
    print("Despawning")

    print("NPC children:")
	for _, child in script.Parent:GetDescendants() do
		if child:IsA("BasePart") then
			print(child:GetFullName())
		end
    end

    task.wait(10)
    for _, part in script.Parent:GetChildren() do
        if part:IsA("BasePart") then
            local tween = TweenService:Create(part, Info, {Transparency = TargetTransparency})
            tween:Play()
            print("Tweened part:", part:GetFullName())
        end
    end
end

print("Child added")
Despawning()
task.wait(13.5)
print("Destroying NPC")
script.Parent:Destroy()

You can remove the debugging prints if you want.

@Nami11255

1 Like

This is working fine Thank you so much

Do you want it to remove accessories as well since I’m almost finished with that?

Also please mark my post as the solution :smile:

No thank you, I intended to make these npc as background civilians in a big city, so other than randomizing color they won’t have an accessory

Okay, well if you need it here you go:

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local TargetTransparency = 1

local function Despawning()
    task.wait(10)
    for _, part in script.Parent:GetChildren() do
        if part:IsA("BasePart") then
            local tween = TweenService:Create(part, Info, {Transparency = TargetTransparency})
            tween:Play()
		end
		
		for _, hat in script.Parent:GetChildren() do
			if hat:IsA("Accessory") then
				for _, handle in hat:GetChildren() do
					if handle:IsA("BasePart") then
						local tween = TweenService:Create(handle, Info, {Transparency = TargetTransparency})
						tween:Play()
					end
				end
			end
		end
    end
end

Despawning()
task.wait(13.5)
script.Parent:Destroy()
1 Like