#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)
print("Hello, "..name)
end)
coroutine.resume(SayHi, "John")
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"))
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
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
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
Thanks!
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