Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make the parts fade all at once?

Hello, Im making a fade effect when a character dies but it fades the parts one by one. How do i make it all at once?

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			for i, v in pairs(char:GetChildren())do
				for i = 1, 10 do
					v.Transparency = v.Transparency + 0.1
					wait()
				end
			end
		end)
	end)
end)

1 answer

✓ Accepted answer

#Spawn(f) According to the ROBLOX Developer page, "Runs the specified callback function in a separate thread, without yielding the current thread." Which means, it will run the function in a separate thread so that you won't have to delay the rest of your script; it acts as if it is its own script.

spawn(function() while wait(5) do print("Foo") end end)
spawn(function() while wait(5) do print("Bar") end end)

Both "Foo" and "Bar" will continue to print side by side, even though both infinite loops are in the same script. It's important to know that there is some slight lag when using spawn. Sometimes the functions take an extra 29 milliseconds or so to actually fire. It's not much, and it won't affect what you're trying to do.


#Coroutines Coroutines is similar to Spawn, it's like it is running its own script, except you can fire the function at a later time. You can also pause or yield coroutines and resume them. Something you cannot do with Spawn.

local SayHi = coroutine.create(function(name) --Creates the coroutine
    print("Hello, "..name)
end)
 
coroutine.resume(SayHi, "John") --Runs the coroutine...
--Similar to Spawn(f), but it's written as coroutine.resume(coroutine) instead.

Hello, John

coroutines.wrap does the same thing as creating a new thread and resuming it at the same time.

local SayHi = coroutine.wrap(function(name)
    print("Hello, "..name)
end)
 
SayHi("John")

Hello, John

Look into the other parts of coroutine like coroutine.yield() or coroutine.status(). They can be very useful!


#IsA IsA is a function of Instance. It will return true if the instance is of a specific class or falls beneath that class.

local wedge = workspace.Wedge
local sphere = workspace.Sphere

print(wedge:IsA("BasePart") == wedge:IsA("BasePart")) --Test to see if the wedge and sphere are both BaseParts (abstract class)
--Boils down to true == true?

true

Every single part in the entire game falls under the abstract class, BasePart. I'm mentioning this because in your code, you are just changing the transparency of every single object in the character. That could cause errors if you are trying to change the transparency of an animation (you can't). You can use an if then statement to prevent the code from changing the transparency of an object if it is not a BasePart.

if part:IsA("BasePart") then
	part.BrickColor = BrickColor.Random()
end

#GetDescendants GetDescendants is the same as GetChildren, except it will now get every single object under Character. Including the weapons and cosmetics the player has equipped.


#Final Product Using Spawn:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			for _, v in pairs(char:GetDescendants()) do
				if v:IsA("BasePart") then
					spawn(function()
						for i = 0,1,.1 do --Simplified this a bit...
							v.Transparency = i
							wait()
						end
					end)
				end
			end
		end)
	end)
end)

Using Coroutines:

local Dissolve = coroutine.wrap(function(part)
	for i = 0,1,.1 do --Simplified this a bit...
		part.Transparency = i
		wait()
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		char.Humanoid.Died:Connect(function()
			for _, v in pairs(char:GetDescendants()) do
				if v:IsA("BasePart") then
				Dissolve(v)
				end
			end
		end)
	end)
end)


Hope it helps!

2 comments

0

Thanks!

0

Now i need to figure out how to make the parts stay transparent, because when i tweaked the parts invisible, it still makes the part half visible then back to invisible when my character died

This discussion is preserved as it was. New answers, comments, and votes are closed.