There is this enemy in my game that I want to fade out, and then delete itself after it dies, it will fade out perfectly, but it won’t despawn
local npc = script.Parent
local human = npc.Humanoid
local head = npc.Head
human.Died:Connect(function()
while true do
wait(.1)
head.Transparency = head.Transparency + 0.05
if head.Transparency == 1 then
npc:Destroy()
end
end)
This can be done pretty easily without loops using Tweens. Here’s what you could do instead:
local npc = script.Parent
local human = npc.Humanoid
local head = npc.Head
local tweenService = game:GetService("TweenService")
local fadeDuration = 2
human.Died:Connect(function()
-- create a tween for the part's transparency and play it
tweenService:Create(head, TweenInfo.new(fadeDuration, Enum.EasingStyle.Linear), {Transparency = 1}):Play()
-- wait until the tween is finished then destroy the NPC
task.wait(fadeDuration)
npc:Destroy()
end)