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

Any way to shorten this script?

Asked by
iNicklas 215 Moderation Voter
8 years ago

I'm proud, its the longest script i have made. Just looks, eh.

yes i realize its vector3.new, thats why i want to shorten it, so its faster to edit :P

local model = script.Parent
local Textl = model.Board.SurfaceGui.TextLabel

while wait(5) do

Num = math.random(1,9)

if Num == 1 then
    Textl.Text  = "Red"
    wait(3)
    model["Really red"].Position = 41, 2, -10
    wait(.2)
    model["Really red"].Position = 41, 1, -10
    wait(.2)
    model["Really red"].Position = 41, 0, -10
    wait(.2)
    model["Really red"].Position = 41, -1, -10
    wait(.2)
    model["Really red"].Position = 41, -2, -10

elseif Num ==  2 then
    Textl.Text  = ""
    wait(3)
    model["Lime green"].Position = 37, 2, -14
    wait(.2)
    model["Lime green"].Position = 37, 1, -14
    wait(.2)
    model["Lime green"].Position = 37, 0, -14
    wait(.2)
    model["Lime green"].Position = 37, -1, -14
    wait(.2)
    model["Lime green"].Position = 37, -2, -14

elseif Num == 3 then
    Textl.Text  = "Pink"
    wait(3)
    model["Magenta"].Position = 45, 2, -14
    wait(.2)
    model["Magenta"].Position = 45, 1, -14
    wait(.2)
    model["Magenta"].Position = 45, 0, -14
    wait(.2)
    model["Magenta"].Position = 45, -1, -14
    wait(.2)
    model["Magenta"].Position = 45, -2, -14

elseif Num == 4 then
    Textl.Text  = "Black"
    wait(3)
    model["Really black"].Position = 45, 2, -10
    wait(.2)
    model["Really black"].Position = 45, 1, -10
    wait(.2)
    model["Really black"].Position = 45, 0, -10
    wait(.2)
    model["Really black"].Position = 45, -1, -10
    wait(.2)
    model["Really black"].Position = 45, -2, -10

elseif Num == 5 then
    Textl.Text  = "White"
    wait(3)
    model["Institutional white"].Position = 41, 2, -14
    wait(.2)
    model["Institutional white"].Position = 41, 1, -14
    wait(.2)
    model["Institutional white"].Position = 41, 0, -14
    wait(.2)
    model["Institutional white"].Position = 41, -1, -14
    wait(.2)
    model["Institutional white"].Position = 41, -2, -14

elseif Num == 6 then
    Textl.Text  = "Blue"
    wait(3)
    model["Really blue"].Position = 37, 2, -18
    wait(.2)
    model["Really blue"].Position = 37, 1, -18
    wait(.2)
    model["Really blue"].Position = 37, 0, -18
    wait(.2)
    model["Really blue"].Position = 37, -1, -18
    wait(.2)
    model["Really blue"].Position = 37, -2, -18

elseif Num == 7 then
    Textl.Text  = "Purple"
    wait(3)
    model["Royal purple"].Position = 45, 2, -18
    wait(.2)
    model["Royal purple"].Position = 45, 1, -18
    wait(.2)
    model["Royal purple"].Position = 45, 0, -18
    wait(.2)
    model["Royal purple"].Position = 45, -1, -18
    wait(.2)
    model["Royal purple"].Position = 45, -2, -18

elseif Num == 8 then
        Textl.Text  = "Yellow"
    wait(3)
    model["New Yellwer"].Position = 37, 2, -10
    wait(.2)
    model["New Yellwer"].Position = 37, 1, -10
    wait(.2)
    model["New Yellwer"].Position = 37, 0, -10
    wait(.2)
    model["New Yellwer"].Position = 37, -1, -10
    wait(.2)
    model["New Yellwer"].Position = 37, -2, -10

elseif Num == 9 then
        Textl.Text  = "Cyan"
    wait(3)
    model["Toothpaste"].Position = 41, 2, -18
    wait(.2)
    model["Toothpaste"].Position = 41, 1, -18
    wait(.2)
    model["Toothpaste"].Position = 41, 0, -18
    wait(.2)
    model["Toothpaste"].Position = 41, -1, -18
    wait(.2)
    model["Toothpaste"].Position = 41, -2, -18

end
end

2 answers

Log in to vote
3
Answered by 8 years ago

Put it into a function! It says in the Tip of the Day you should use Functions if you're trying to do something multiple times. Instead of typing the same code, you can just refer to one of your functions! Functions have things called parameters. They can be anything from parts to players to colors.

function color(part)
    part.BrickColor = BrickColor.Random()
end

color(workspace.Part)
color(workspace.BasePlate)
color(workspace.Part2)

This is what we'll be using in your script.


Final Product

After editing I removed the function

local model,Textl = script.Parent, model.Board.SurfaceGui.TextLabel
colors = {"Really red", "Lime green", "Magenta", "Really black", "Institutional white", "Really blue", "Royal purple", "New Yeller", "Toothpaste"} texts = {"Red", "Green", "Pink", "Black", "White", "Blue", "Purple", "Yellow", "Cyan"}
while true do
Num = math.random(1,9)
    Textl.Text  = texts[Num]
    wait(8)
    for i = 2,-2,-1 do
        model[colors[Num]].CFrame = CFrame.new(model[colors[Num]].CFrame.X,i,model[colors[Num]].CFrame.Z)
        wait(.2)
    end
end

Additional Things

  • Use CFrame instead of position
  • When you're changing position use Vector3
  • You spelled "New Yeller" wrong. Is it supposed to be wrong? If so, change it back.


Hope it helps!

Ad
Log in to vote
0
Answered by
davness 376 Moderation Voter
8 years ago

Yes, you can do it. Just use a table!

local model = script.Parent -- get instances
local Textl = model.Board.SurfaceGui.TextLabel

local tablex = {"Really red", "Lime green", "Magenta", "Really black", "Institutional white", "Really blue", "Royal purple", "New Yellwer", "Toothpaste"} -- get the parts' names
local Tex = {"Red", "", "Pink", "Black", "White", "Blue", "Purple", "Yellow", "Cyan"} -- get he texts' names

while wait(5) do
    num = math.random(1, 9) -- draw a number
    part = tablex[num] -- get a part  from the drawn number
    Textl.Text = Tex[num] -- get a text value from the same value drawn
    wait(3)
    for i = 2,-2,-1 do
        model[part].CFrame = CFrame.new(model[part].Position.X, i, model[part].Position.Z) -- move the part on Y axis
        wait(0.2)
    end
end
0
If you want it to be a competition, You use Position, position won't clip through parts, therefore in this person's minigame, the parts wont go under the lava/water. EzraNehemiah_TF2 3552 — 8y

Answer this question