Get all childerns to do something

in this script:

	for _,Part in next(), monstr.Model:children() do
	  if Part:IsA('BasePart') then
		local f = Instance.new("ParticleEmitter")
		f.SpreadAngle = Vector2.new(100,100)
		f.Lifetime = NumberRange.new(1,1)
		f.Texture = "http://www.roblox.com/asset/?id=431361643"
		f.Parent = Part
	    for i = 0,1, 0.1 do
		wait()
		Part.Transparency = i
		print(i)
		end
		f:Destroy()
		end
	end

how i make it apply to all children at once and not for every next children?

im pretty sure for i,v in pairs would apply to all children at once
(though this may not be true)

:children() is deprecated, you should use :GetChildren() instead Instance | Documentation - Roblox Creator Hub And you can do

 for x,y in pairs(monstr.Model:GetChildren()) do
-- code here
end;

i did this:

	for x,y in pairs(monstr.Model:GetChildren()) do
		local f = Instance.new("ParticleEmitter")
		f.SpreadAngle = Vector2.new(100,100)
		f.Lifetime = NumberRange.new(1,1)
		f.Texture = "http://www.roblox.com/asset/?id=431361643"
		f.Parent = y
	    for i = 0,1, 0.1 do
		wait()
		y.Transparency = i
		print(i)
		end
		f:Destroy()
		end

it still does that thing…
https://gyazo.com/dca16d5b3f8819242caa21eeb63264fb

1 Like

When you say next children do you mean this?

 for x,y in pairs(monstr.Model:GetDescendants()) do
-- code here
end;

i mean i want to make all parts disappear at once

There’s two ways of doing this:

  1. Create a coroutine for each part so that the waits are run asynchronously:
for _, Part in pairs(monstr.Model:GetChildren()) do
    if Part:IsA("BasePart") then
        -- create f
        f.Parent = Part

        coroutine.wrap(function()
            for i = 0, 1, 0.1 do
                Part.Transparency = i
                wait()
            end

            f:Destroy()
        end

    end
end
  1. Run the for loops methodically so that they all use one wait:
for _, Part in pairs(monstr.Model:GetChildren()) do
    if Part:IsA("BasePart") then
        -- create f
        f.Parent = Part
    end
end

for i = 0, 1, 0.1 do
    for _, Part in pairs(monstr.Model:GetChildren()) do
        if Part:IsA("BasePart") then
            Part.Transparency = i
        end
    end
    wait()
end

for _, Part in pairs(monstr.Model:GetChildren()) do
    if Part:IsA("BasePart") then
        -- effect is obviously not called f
        Part.f:Destroy()
    end
end

It’s probably easier to clean up the array before continuing though:

local children = {}
for _, Part in pairs(monstr.Model:GetChildren()) do
    if Part:IsA("BasePart") then
        children[#children + 1] = Part
    end
end

for _, Part in pairs(children) do
    -- create f
    f.Parent = Part
end

for i = 0, 1, 0.1 do
    for _, Part in pairs(children) do
        Part.Transparency = i
    end

    wait()
end

for _, Part in pairs(children) do
    Part.f:Destroy()
end

The comments are signs of pseudocode, you can flesh it out so that it makes sense.

2 Likes

iirc I am pretty sure you can’t do this without spawn(function().

Scripts on roblox run on 1 thread, so this means it will do everything one at a time.

To accomplish this here is what you would do:


spawn(function()
    for _,Part in next(), monstr.Model:children() do
	  if Part:IsA('BasePart') then
		local f = Instance.new("ParticleEmitter")
		f.SpreadAngle = Vector2.new(100,100)
		f.Lifetime = NumberRange.new(1,1)
		f.Texture = "http://www.roblox.com/asset/?id=431361643"
		f.Parent = Part
	    for i = 0,1, 0.1 do
		wait()
		Part.Transparency = i
		print(i)
		end
		f:Destroy()
		end
	end
end)

the first one didn’t set the transparency to i and the second one gives an error that “f is not a valid member of WedgePart”

You could also do

local ts = game:GetService("TweenService");

for x,y in pairs(monstr.Model:GetDescendants()) do
    if y:IsA("BasePart") then
	
	local f = Instance.new("ParticleEmitter");
	f.SpreadAngle = Vector2.new(100,100);
	f.Lifetime = NumberRange.new(1,1);
	f.Texture = "http://www.roblox.com/asset/?id=431361643";
	f.Parent = y;

	local tween = ts:Create(y, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0), {Transparency = 1;});
	tween.Completed:Connect(function()
		f:Destroy();
	end);
	tween:Play();
	
	end;
end;

or what @goldenstein64 said

1 Like

oh your worked, thank you, and thanks @goldenstein64

2 Likes

Tweens have an event that signals a tween’s end, Completed. Instead of using the DebrisService, you can destroy the object when Completed fires.

-- Create tween
tween.Completed:Connect(function ()
    f:Destroy()
end)
tween:Play()
1 Like

My bad, I edited my reply to use tween.Completed instead of using DebrisService. Thanks!