Cannot make script to fade out and delete part after death of humanoid

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So i have part in model with humanoid, i want to make my script to fade out part “head” and destroy it (better if delete whole model)
    изображение

  2. What is the issue? Include screenshots / videos if possible!
    My script works, but when i try to add lines with any way of fading out, or deleting part, with loop or without, my scripts refuses to work, sometimes in really confusing times such as making “head” part not unanchor to fall, or stop sound from playing. Im really confused with it.

local humanoid = script.Parent.Humanoid
local plank = script.Parent
local part = script.Parent.Head
local Damage = script.Parent.Head.Damage
local Break = script.Parent.Head.Break
local soundplaying = false

if humanoid.Health <= 0 then
	wait(3)
	script.Parent.Head:Destroy()
end

humanoid.HealthChanged:Connect(function()
	if soundplaying == false then
		if humanoid.Health <= 0 then
			soundplaying = true
			Break.PlaybackSpeed = math.random(0.8, 1.3)
			Break:Play()
			wait(0.5)
			part.Anchored = false
			wait(0.5)
			part.ded.Value = true
			soundplaying = false
		elseif humanoid.Health > 0 then
			soundplaying = true
			Damage.PlaybackSpeed = math.random(0.8, 1.3)
			Damage:Play()
			wait(0.2)
			soundplaying = false
		end
		
	end
end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I viewed around 10 topics already, and havent found any working solution, i tried to make it simply change transparency, even inside of “break” part of script that makes plank emit break sound and unanchor, tried with loops, without loops, with humanoid.died:connect(function() end) and with bool value to run script in different script file. Nothing worked, i get errors only when script is written with issues, but when it doesnt, and i test, it doesnt show any errors in console, but changing with command bar does work. Its a server-sided script.
    Please help me, i lost all motivation and wasted a lot of time.

One solution you can try is using the “TweenService” module to gradually fade out the transparency of the “Head” part over a set period of time before destroying it. Here’s an example code snippet that you can modify and incorporate into your existing script:

local TweenService = game:GetService("TweenService")
local part = script.Parent.Head

-- Fade out the part over 3 seconds
local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local transparencyTween = TweenService:Create(part, tweenInfo, { Transparency = 1 })

-- Play the transparency tween and destroy the part
humanoid.Died:Connect(function()
    transparencyTween:Play()
    wait(3)  -- Wait for the tween to finish
    part:Destroy()
end)

I hope that was helpful. If you have any other queries, let me know.

1 Like

I’ll just add, instead of using wait to wait for the tween to finish you can just use:

transparencyTween.Completed:Wait()
1 Like

I still get my plank to work properly but not to fade out.

The issue could be related to the scope of the “part” variable. Ensure you use the same “part” variable in both parts of the code. Also, check that the “Transparency” property of the “Head” part is set to 0 by default. If it is not, you can set it to 0 manually in the properties tab.

local part = script.Parent.Head

-- Check that Transparency is set to 0
part.Transparency = 0
1 Like

Everything is fine, i just tested it on new baseplate to be sure if anything is interrupting scripts, but it didnt work again, i dont understand why does not it work.

I think you need change > to >=

It does make sound play only if health changed, and its value still above 0, while part of script above with <= 0 does make sound of plank breaking apart and falling down, so i dont think just adding = to < would help.

I think i fixed your script with some parts of @76IOOO and @Prince_Duke scripts.
I just tested it and it works:

local humanoid = script.Parent.Humanoid
local plank = script.Parent
local part = script.Parent.Head
local Damage = script.Parent.Head.Damage
local Break = script.Parent.Head.Break
local soundplaying = false

local TweenService = game:GetService("TweenService")
local think = {
	Transparency = 1
}
-- Fade out the part over 3 seconds
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local transparencyTween = TweenService:Create(part, tweenInfo, think)

-- Play the transparency tween and destroy the part
humanoid.HealthChanged:Connect(function()
	if soundplaying == false then
		if humanoid.Health <= 0 or humanoid.Health == 0 then
			soundplaying = true
			Break.PlaybackSpeed = math.random(0.8, 1.3)
			Break:Play()
			wait(0.5)
			part.Anchored = false
			transparencyTween:Play()
			transparencyTween.Completed:Wait()  -- Wait for the tween to finish
			part:Destroy()
		elseif humanoid.Health >= 0 then
			soundplaying = true
			Damage.PlaybackSpeed = math.random(0.8, 1.3)
			Damage:Play()
			wait(0.2)
			soundplaying = false
		end
	end
end)

If it doesn’t work let me know.

To everyone on this post: please use humanoid.died instead.

Thx, worked. Now i can continue making my game.

1 Like

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