How do I get all the parts and children of models in workspace?

I was working on a rainbow part script, and then I though it would be cool if I could get everything in Workspace, and make it rainbow…

Here is what I have so far.

for _,part in pairs(workspace:GetChildren()) do --NEED HELP HERE
    if part:IsA("BasePart") then --NEED HELP HERE
        end
        while true do  --no need to mess with past here...
            wait(1) 
            wait(0.1) 
            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(255, 0, 0)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(255, 155, 0)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(255, 255, 0)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(0, 255, 0)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(0, 255, 255)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(0, 155, 255)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(255, 0, 255)}):Play() 
            wait(1.1)

            game:GetService('TweenService'):Create(
            part,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut),
            {Color = Color3.fromRGB(255, 0, 155)}):Play() 
            wait(1.1)


        end 
    end
end


Thanks!

Constantly tweening every part in workspace would be really performance-heavy

-- you can use workspace:GetDescendants() if you want to get parts that could be within folders in workspace or inside other objects, etc.
for i, part in pairs(workspace:GetChildren()) do
    if part:IsA'BasePart' then
        -- make a new coroutine so the while loop doesn't stop script execution
        coroutine.wrap(function()
            while true do
                -- put tween loop here
            end
        end)()
    end
end
2 Likes

Base off @heII_ish’s concept of the coroutine to have each part tween and not just 1 at a time and what you are looking for is :GetDescendants()

for _,part in pairs(workspace:GetDescendants()) do -- GetDescendants
    if part:IsA("BasePart") then -- Good

That should take every Instance object in workspace.

1 Like

what… sorry I’m new… did I do something wrong… sorry if I did.

No i deleted my post lol. Since someone else said the same thing as me.

2 Likes

This is probably the most efficient way of doing this: (adding onto @hell_ish’s solution)

local TweenService = game:GetService("TweenService")

local function RandomColor3()
	return Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))
end
for i, part in pairs(workspace:GetDescendants()) do
	if part:IsA("BasePart") then
		coroutine.wrap(function()
			while true do
				local tween = TweenService:Create(part, TweenInfo.new(.3, Enum.EasingStyle.Linear), { Color = RandomColor3() })
				tween:Play()
				tween.Completed:Wait()
			end
		end)()
	end
end

You should also do this on the client for best performance, just put it in a local script inside StarterPlayerScripts.

2 Likes

:GetDescendants() gets everything in the instance. I believe this isn’t working for you because this is running prior to your character loading in. Try including a wait(6) at the top of the script.

2 Likes

Just added a wait(6) and it works! Thanks so much! I didn’t know what to expect! since this is my first post!