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

How do I make this script smaller and more simple?

Asked by 4 years ago

Trying to tween a block to follow a track using tween service, it works but it takes too long to script so I just want the script to make it go through each tween every 0.6 seconds like this:

local ts = game:GetService("TweenService")

local part = script.Parent

local Tweeninf = TweenInfo.new( 0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0.00000000000000000000000000000000000000000000000001 ) local move = { CFrame = game.Workspace.Track1.CFrame } local move2 = {CFrame = game.Workspace.Track2.CFrame } local move3 = {CFrame = game.Workspace.Track3.CFrame } local move4 = {CFrame = game.Workspace.Track4.CFrame } local move5 = {CFrame = game.Workspace.Track5.CFrame } local move6 = {CFrame = game.Workspace.Track6.CFrame } local move7 = {CFrame = game.Workspace.Track7.CFrame } local move8 = {CFrame = game.Workspace.Track8.CFrame } local move9 = {CFrame = game.Workspace.Track9.CFrame } local move10 = {CFrame = game.Workspace.Track10.CFrame } local move11 = {CFrame = game.Workspace.Track11.CFrame } local move12 = {CFrame = game.Workspace.Track12.CFrame } local move13 = {CFrame = game.Workspace.Track13.CFrame } local move14 = {CFrame = game.Workspace.Track14.CFrame } local move15 = {CFrame = game.Workspace.Track15.CFrame } local move16 = {CFrame = game.Workspace.Track16.CFrame } local move17 = {CFrame = game.Workspace.Track17.CFrame }

local tween1 = ts:Create(part,Tweeninf,move) local tween2 = ts:Create(part,Tweeninf,move2) local tween3 = ts:Create(part,Tweeninf,move3) local tween4 = ts:Create(part,Tweeninf,move4) local tween5 = ts:Create(part,Tweeninf,move5) local tween6 = ts:Create(part,Tweeninf,move6) local tween7 = ts:Create(part,Tweeninf,move7) local tween8 = ts:Create(part,Tweeninf,move8) local tween9 = ts:Create(part,Tweeninf,move9) local tween10 = ts:Create(part,Tweeninf,move10) local tween11 = ts:Create(part,Tweeninf,move11) local tween12 = ts:Create(part,Tweeninf,move12) local tween13 = ts:Create(part,Tweeninf,move13) local tween14 = ts:Create(part,Tweeninf,move14) local tween15 = ts:Create(part,Tweeninf,move15) local tween16 = ts:Create(part,Tweeninf,move16) local tween17 = ts:Create(part,Tweeninf,move17)

a = 0.6 ? while true do wait(a) tween:Play() wait(a) tween:Play() wait(a) tween2:Play() wait(a) tween3:Play() wait(a) tween4:Play() wait(a) tween5:Play() wait(a) tween6:Play() wait(a) tween7:Play() wait(a) tween8:Play() wait(a) tween9:Play() wait(a) tween10:Play() wait(a) tween11:Play() wait(a) tween12:Play() wait(a) tween13:Play() wait(a) tween14:Play() wait(a) tween15:Play() wait(a) tween16:Play() wait(a) tween17:Play() wait(a) end

Can I make the last bit more simple where it plays through them, like do an array or something ?? I'm not that experienced Thank you

2 answers

Log in to vote
0
Answered by 4 years ago

I wish you used a code block, but I think I understand what you're trying to do.

Use a for loop.

For the variables you could use a table.

move = {}
for i = 1, 17 do --the 17 is the total number of tracks u have
    move[i] = {CFrame = workspace["Track"..i].CFrame}
end

That gets all of the cframes of the tracks. Hopefully you understand the gist of it. You can also use another for loop to tween.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You could have a table for your tweens and then use an in ipairs loop.

local table = { -- This is how you make a table. You don't have to put them on separate lines like I did. I just wanted it to look neater

    tween1,
    tween2,
    tween3,
    tween4,
    tween5,
    tween6,
    tween7,
    tween8,
    tween9,
    tween10,
    tween11,
    tween12,
    tween13,
    tween14,
    tween15,
    tween16,
    tween17
}

while true do
    for i, tween in ipairs(table) do -- ipairs will loop through all of the tweens in order
        local a = 0.6
        wait(a)
        tween:Play()
    end
end

Hope this helps!


Infinite Tweens

local tween = {}

for i = 1, math.huge, 1 do -- math.huge gets infinity. This is a for loop by the way
    local tweenClone = tween1:Clone()
    table.insert(tween, tweenClone)

You will NEVER want to do this because the game can never load an infinite amount of Instances or anything.

0
Thanks a lot it does but how would I make the table go from tween1 to infinite tweens? ATRGuestServices 5 — 4y
0
I just added something to the post that will teach you how to do infinite tweens. AntiWorldliness 868 — 4y

Answer this question