Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
gr3uh 16
3 years ago
Edited 3 years ago

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



0
Use :GetDescendants() FrontsoldierYT 129 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Someone here already answered your question: GetDescendants()

Hey man by the way, you shouldn't use the same code over and over again. You should optimize your scripts by creating functions for reusability, just thought I'd leave a tip.

0
Thank you! gr3uh 16 — 3y
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
3 years ago
Edited 3 years ago

The question has already been answered, but it is not enough to make it work. To make this work you will need coroutines. But first few tips.

First of all Rainbow effect, just screams to use Color3.fromHSV constructor. By manipulating H value, you can easily get all rainbow colours in right order.

Second of all, unless you only have few parts in workspace this will lag A LOT. It is good idea to prepare as much as you can outside the main loop.

Finally you are going to have to create need separate coroutines, unless you set your tweens to repeat on their own. Sometimes infinite tweens are enough, but in this case there is no way to set single tween to go through all colours on its own since Roblox does not use HSV Colours by default.

Without going to deep into coroutines, lets just use spawn this time. Think of whatever is in spawn like a separate script running. We will have separate "script" for each part. Again do not do this for large map, everything will just lag horribly otherwise.

local TS = game:GetService("TweenService")
local style = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)

local targets = {}
local h = 0
repeat
    table.insert(targets,Color3.fromHSV(h,1,1)) 
    h = h + 0.1
until h > 1 


for _, part in pairs(workspace:GetDescendants()) do
    if part:IsA("BasePart") then
        spawn(function()
            local tweens = {}
            for _, target in ipairs(targets) do
                table.insert(tweens,TS:Create(part,style,{Color = target}))
            end
            local i = 1
            while true do
                tweens[i]:Play()
                tweens[i].Completed:Wait()
                i = i + 1
                if i > #tweens then
                    i = 1
                end
            end
        end)
        wait(1) --make wait so that parts do not have the same color at the same time
    end
end

Have fun!

Answer this question