How to make a script continue even while a function is going off

I have this script that is supposed to make the player disappear into ashes upon death, it makes the limbs black and fade away along with particles, I want them to all disappear and whatever all at once, but they just disappear and whatever one by one. How do I make them do the thing all at once?

script:

local Dissapear = function(char)
	for _,i in pairs(char:GetChildren()) do
		if i:IsA("BasePart") or i:IsA("Accessory") then
			if i ~= "HumanoidRootPart" then
				local Ash = AshParticleEmmitter:Clone()
				Ash.Parent = i

				if i:IsA("BasePart") then
					i.Color = Color3.fromRGB(0,0,0)
					TS:Create(i, TI, {Transparency = 1}):Play()
				end
				wait(1.3)
				i:Destroy()
			end
		end
	end
end



Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local char = plr.Character or plr.CharacterAdded:Wait()
		local hum = char:FindFirstChild("Humanoid")
		hum.Died:connect(function()
			PlayerDiedEvent:FireClient(plr)
			
				Dissapear(char)
			
		end)
	end)
end)
1 Like

Try just having wait() instead of wait(1.3). Or try removing the wait all together.

1 Like

Try using a task.delay like this

task.delay(1.3, function)
i:destroy()

something like that.

1 Like

There are basically 4 options for this particular scenario: spawn, coroutine, delay and debris:


Spawn

task.spawn (spawn itself is deprecated, so you should use task.spawn instead) allows for code to be carried out while not interrupting the other code in the script.

It takes the function as the first parameter, and any arguments as the subsequent parameters
e.g.

local function DoStuff(Message)
    task.wait(1)
    print(DoStuff)
    task.wait(2)
    print("End of function")
end

task.spawn(DoStuff, "Hello World!")
print("Roblox!")

This would output Roblox!, then Hello World!, then finally End of function


Coroutines

Coroutines behave similarly to spawn.
I’ve not had to deal with coroutines much (preferring to use spawn instead) so I’m not an expert, but the basic usage would be like this:
e.g.

local function DoStuff(Message)
    task.wait(1)
    print(DoStuff)
    task.wait(2)
    print("End of function")
end

local Routine = coroutine.create(DoStuff("Hello World!"))
Routine.resume()
print("Roblox!")

This would have the same output as spawn.


Delay

Delay will wait a set number of seconds before running a function, without interupting the main flow of code:
task.delay(TIME, FUNCTION, ARGUMENTS)
e.g.

local function Send(Message)
    print(Message)
end

task.delay(1, Send, "Hello World!")
print("Ended")

This would output Ended, then a second later will output Hello World!


Debris

Debris destroys an object after a set amount of time, without interupting the main flow of code:

local Debris = game:GetService("Debris")
Debris:AddItem(Object, Time)

e.g.

local Debris = game:GetService("Debris")
local Part = game.Workspace.Part

Debris:AddItem(Debris, 3)

This removes the part after 3 seconds


You can pretty much just choose which one you want to use.

Where the method requires a function, you can define a function within the method:
e.g.

task.spawn(function()
    wait(1.3)
    i:Destroy()
end))
3 Likes

Worked like a charm! ima also write down everything else you wrote, cuz damn dude, thats a lotta helpful info!

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