Shortening Repeated Code

I want to shorten this code, but the problem is that it requires to go through different parts at a time. I looked at for loops, but they don’t seem to solve the problem.
Here is the code:

Code
local TweenService = game:GetService("TweenService")

--- Parts
local Model = workspace.Game.Islands.Spawn.Objects.Boat
local PartRoot = Model.Root
local Path = workspace.Game.BoatPath

--- Configurations
local TurnInfo = TweenInfo.new(
5,
Enum.EasingStyle.Quint,
Enum.EasingDirection.InOut,
0,
false,
0
)

local CheckpointInfo = TweenInfo.new(
20,
Enum.EasingStyle.Quint,
Enum.EasingDirection.InOut,
0,
false,
0
)

--- Wait Times
local Turn = 5
local Move = 20
local Dock = 5

--- Tweens
local C1 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint1.CFrame}) --- Dock at Spawn
local C2 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint2.CFrame})
local C3 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint3.CFrame})
local C4 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint4.CFrame})
local C5 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint5.CFrame})
local C6 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint6.CFrame})
local C7 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint7.CFrame})
local C8 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint8.CFrame}) --- Dock at Showcase Island
local C9 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint9.CFrame})
local C10 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint10.CFrame})
local C11 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint11.CFrame})
local C12 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint12.CFrame})
local C13 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint13.CFrame})
local C14 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint14.CFrame})
local C15 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint15.CFrame}) --- Dock at Lighthouse Island
local C16 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint16.CFrame})
local C17 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint17.CFrame})
local C18 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint18.CFrame})
local C19 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint19.CFrame})
local C20 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint20.CFrame})
local C21 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint21.CFrame})
local C22 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint22.CFrame})
local C23 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint23.CFrame})

--- Process
wait(5)
while true do
C2:Play()
wait(Move)
C2:Cancel()
C3:Play()
wait(Turn)
C3:Cancel()
C4:Play()
wait(Move)
C4:Cancel()
C5:Play()
wait(Turn)
C5:Cancel()
C6:Play()
wait(Move)
C6:Cancel()
C7:Play()
wait(Turn)
C7:Cancel()
C8:Play()
wait(Move + Dock)
C8:Cancel()
C9:Play()
wait(Move)
C9:Cancel()
C10:Play()
wait(Turn)
C10:Cancel()
C11:Play()
wait(Move)
C12:Play()
wait(Turn)
C12:Cancel()
C13:Play()
wait(Move)
C13:Cancel()
C14:Play()
wait(Turn)
C14:Cancel()
C15:Play()
wait(Move + Dock)
C15:Cancel()
C16:Play()
wait(Move)
C16:Cancel()
C17:Play()
wait(Turn)
C17:Cancel()
C18:Play()
wait(Move)
C18:Cancel()
C19:Play()
wait(Turn)
C19:Cancel()
C20:Play()
wait(Move)
C20:Cancel()
C21:Play()
wait(Turn)
C21:Cancel()
C22:Play()
wait(Move)
C22:Cancel()
C23:Play()
wait(Turn)
C23:Cancel()
C1:Play()
wait(Move + Dock)
C1:Cancel()
end

I would appreciate any help. Thanks in advance! :slight_smile:

Made the code above more compact without any unnecessary lines.

1 Like

You can shorten

This part
local C1 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint1.CFrame}) --- Dock at Spawn

local C2 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint2.CFrame})

local C3 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint3.CFrame})

local C4 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint4.CFrame})

local C5 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint5.CFrame})

local C6 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint6.CFrame})

local C7 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint7.CFrame})

local C8 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint8.CFrame}) --- Dock at Showcase Island

local C9 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint9.CFrame})

local C10 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint10.CFrame})

local C11 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint11.CFrame})

local C12 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint12.CFrame})

local C13 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint13.CFrame})

local C14 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint14.CFrame})

local C15 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint15.CFrame}) --- Dock at Lighthouse Island

local C16 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint16.CFrame})

local C17 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint17.CFrame})

local C18 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint18.CFrame})

local C19 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint19.CFrame})

local C20 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint20.CFrame})

local C21 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint21.CFrame})

local C22 = TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint22.CFrame})

local C23 = TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint23.CFrame})

To :

Cs = {}

   for c  = 1, 23 do 
   Cs["C"..c] =  TweenService:Create(PartRoot, TurnInfo, {CFrame = Path["Checkpoint"..c].CFrame})  -- e.g, C1, C2 etc.
end

you will have to change however how you access those “variables”, for instance

 print(Cs["C1"]) -- >  TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint1.CFrame})
-- for C1

I don’t think this would work either as different movements have different tween infos, so assigning the same one to all of them is not what I want.

I meant the TweenInfo. (e.g. TurnInfo, MoveInfo)

that is also different as ,

I assign each Checkpoint the name Checpoint n

where n is = to the value of c, as in for each string index, the value of the Tween also corresponds to it’s own tween as before.

Edit : I see what you mean, I’ll edit

1 Like

Hey, just made this code for you. Tested it, and it works. All you have to do is put 2 BoolValues into your Checkpoints named Turn and Dock and then set the values accordingly.

(I tested with 8 checkpoints, but it works with as many as you put in the Path folder. :wink: )

If the script sees Turn.Value == true, it will use the turn TweenInfo, and if it’s false it will use CheckpointInfo.

local TweenService = game:GetService("TweenService")

--- Parts
local Model = workspace.Game.Islands.Spawn.Objects.Boat
local PartRoot = Model.Root
local Path = workspace.Game.BoatPath

--- Configurations
local TurnInfo = TweenInfo.new(
	5,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local CheckpointInfo = TweenInfo.new(
	20,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

--- Wait Times
local Turn = 5
local Move = 20
local Dock = 5

--- Tweens
local Tweens = {};
for _, pathPart in ipairs(Path:GetChildren()) do
	local tween = TweenService:Create(
		PartRoot,
		(pathPart:FindFirstChild("Turn").Value == true and TurnInfo or CheckpointInfo),
		{CFrame = pathPart.CFrame}
	);
	
	table.insert(Tweens, string.match(pathPart.Name, "%d+"), {
		Tween = tween;
		Dock = pathPart:FindFirstChild("Dock").Value;
	}); --Set our tween into the table at it's checkpoint number
end;
--[[
	TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint1.CFrame}); --- Dock at Spawn
	TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint2.CFrame});
	TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint3.CFrame});
	TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint4.CFrame});
	TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint5.CFrame});
	TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint6.CFrame});
	TweenService:Create(PartRoot, TurnInfo, {CFrame = Path.Checkpoint7.CFrame});
	TweenService:Create(PartRoot, CheckpointInfo, {CFrame = Path.Checkpoint8.CFrame}); --- Dock at Showcase Island
--]]

--- Process
wait(5)
while true do
	for tweenNumber, tweenData in pairs(Tweens) do
		tweenData.Tween:Play();
		tweenData.Tween.Completed:Wait();
		if tweenData.Dock then
			wait(Dock);
		end;
	end;
end

I also condensed the code running the tweens. It waits for the tween to finish, instead of using the time and then cancelling it, as well as waiting for dock if the Checkpoint.Dock.Value == true.

Hope this helps!

1 Like