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

How do I make these scripts into only one script for like many parts?

Asked by 9 years ago

To make you understand more here: I made like at least 50 scripts for all 50 parts because I wanted to make the parts like colorful patterns. I want to use maybe only one script to function all of the parts like these scripts. I also had to number all the parts for the specific ones that needs to be in patterns. These are the three scripts that I did for all (about) 50 parts:

For Red:

local p = game.Workspace.Frame3.Part10

while true do
p.BrickColor = BrickColor.White()
wait(0.1)
p.BrickColor = BrickColor.Green()
wait(0.1)
p.BrickColor = BrickColor.Red()
wait(0.1)
end

For White:

local p = game.Workspace.Frame3.Part11

while true do
p.BrickColor = BrickColor.Green()
wait(0.1)
p.BrickColor = BrickColor.Red()
wait(0.1)
p.BrickColor = BrickColor.White()
wait(0.1)
end

For Green:

local p = game.Workspace.Frame3.Part12

while true do
p.BrickColor = BrickColor.Red()
wait(0.1)
p.BrickColor = BrickColor.White()
wait(0.1)
p.BrickColor = BrickColor.Green()
wait(0.1)
end

Hope you understand what I am asking.

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Copy Scripts

Define p as something like script.Parent. Make another script that clones that script into all of the children of workspace.Frame3.

This isn't really a solution, but it does mean you won't have to change lots of scripts just because you want to try something different.


Functions

The code is the same thing for every p -- so we can just write a function.

function flash(p)
    while true do
        p.BrickColor = BrickColor.Red()
        wait(0.1)
        p.BrickColor = BrickColor.White()
        wait(0.1)
        p.BrickColor = BrickColor.Green()
        wait(0.1)
    end
end

then call that function for all of the children:

for _, child in pairs(workspace.Frame3:GetChildren()) do
    flash(child)
end

This won't actually work, though. That's because the first while loop won't ever stop. We can spawn new coroutines to solve this:

for _, child in pairs(workspace.Frame3:GetChildren()) do
    spawn(function() flash(child) end)
end

This will work (and actually works in a very similar way to just copying the scripts), but it isn't necessarily elegant.

One Loop

It depends on exactly what the pattern you want is. For instance, if you want them all to be the same color, you could do something like this:

function paint(bricks, color)
    for _, brick in pairs(bricks) do
        brick.BrickColor = color    
    end
end

while true do
    paint( workspace.Frame3:GetChildren(), BrickColor.Red())
    wait(0.1)
    paint( workspace.Frame3:GetChildren(), BrickColor.White())
    wait(0.1)
    paint( workspace.Frame3:GetChildren(), BrickColor.Green())
end

But that will be boring.

You could get finer control, though:

function update(brick, data)
    local name = brick.Name
    local number = tonumber(name:match("%d+"))
    -- Get the name in the brick
    -- In general, it would be better for the script to figure
    -- this out on its own, e.g., from a position
    local modulo = (data + number) % 3
    -- this will be 0, 1, or 2. It goes to the next one when you increase
    -- either number or data by 1 (and from 2 goes back to 0).
    if modulo == 0 then
        brick.BrickColor = BrickColor.White()
    elseif modulo == 1 then
        brick.BrickColor = BrickColor.Green()
    else
        brick.BrickColor = BrickColor.Red()
    end
end

function updateBricks(bricks, data)
    for _, brick in pairs(bricks) do
        update(brick, data)
    end
end

while true do
    for i = 1, 3 do
        wait(0.1)
        updateBricks( workspace.Frame3:GetChildren(), i)
    end
end

You could do of course a lot more.

It comes down to some "update" function. It needs to figure out, "at this moment, for this brick, what color do I need to pick?" Then you just need to call update with whatever information it needs.

Here's another update function, as an example:

function update(brick)
    local mag = brick.Position.magnitude
    local amt = 0.5 + 0.5 * math.cos( mag / 20 + tick() )
    brick.BrickColor = BrickColor.new( Color3.new(amt, amt, amt) )
end
0
Thanks man User#5689 -1 — 9y
Ad

Answer this question