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
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
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
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
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
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
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
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()
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()