How do I make this code shorter?

Hi, I just created a relatively long code and I’m about one hundred percent sure it could be shorter.

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

local part: Part = script.Parent

local positions = {
	["0"] = part.Position,
	["1"] = Vector3.new(-336.664, 6.061, -2165.951),
	["2"] = Vector3.new(-331.664, 6.061, -2135.951),
	["3"] = Vector3.new(-326.664, 6.061, -2165.951),
	["4"] = Vector3.new(-321.664, 6.061, -2135.951),
	["5"] = Vector3.new(-316.664, 6.061, -2165.951),
	["6"] = Vector3.new(-311.664, 6.061, -2135.951),
	["7"] = Vector3.new(-306.664, 6.061, -2165.951),
	["8"] = Vector3.new(-301.664, 6.061, -2135.951),
	["9"] = Vector3.new(-296.664, 6.061, -2165.951),
	["10"] = Vector3.new(-291.664, 6.061, -2135.951),
	["11"] = Vector3.new(-286.664, 6.061, -2165.951),
	["12"] = Vector3.new(-281.664, 6.061, -2135.951),
	["13"] = Vector3.new(-276.664, 6.061, -2165.951),
	["14"] = Vector3.new(-271.664, 6.061, -2135.951),
	["15"] = Vector3.new(-266.664, 6.061, -2165.951),
	["16"] = Vector3.new(-261.664, 6.061, -2135.951),
	["17"] = Vector3.new(-256.664, 6.061, -2165.951),
	["18"] = Vector3.new(-251.664, 6.061, -2135.951),
	["19"] = Vector3.new(-246.664, 6.061, -2165.951),
	["20"] = Vector3.new(-241.664, 6.061, -2135.951),
	["21"] = Vector3.new(-236.664, 6.061, -2165.951),
	["22"] = Vector3.new(-231.664, 6.061, -2135.951),
	["23"] = Vector3.new(-226.664, 6.061, -2165.951),
	["24"] = Vector3.new(-221.664, 6.061, -2135.951),
	["25"] = Vector3.new(-216.664, 6.061, -2165.951),
	["26"] = Vector3.new(-211.664, 6.061, -2135.951),
	["27"] = Vector3.new(-206.664, 6.061, -2165.951),
	["28"] = Vector3.new(-201.664, 6.061, -2135.951),
}

local tweenInfo0: TweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut,
	-1,
	true
)

while true do
	TweenService:Create(part, tweenInfo0, {Position = positions["1"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["2"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["3"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["4"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["5"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["6"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["7"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["8"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["9"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["10"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["11"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["12"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["13"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["14"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["15"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["16"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["17"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["18"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["19"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["20"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["21"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["22"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["23"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["24"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["25"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["26"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["27"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["28"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["27"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["26"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["25"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["24"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["23"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["22"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["21"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["20"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["19"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["18"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["17"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["16"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["15"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["14"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["13"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["12"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["11"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["10"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["9"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["8"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["7"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["6"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["5"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["4"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["3"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["2"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["1"]}):Play()	
	task.wait(tweenInfo0.Time)
	TweenService:Create(part, tweenInfo0, {Position = positions["0"]}):Play()	
	task.wait(tweenInfo0.Time)
end
2 Likes

is it a module script, or a local script, or a server script??

It’s a server script inside a part.

1 Like

you can try adding a table! that does all of it each!

1 Like

Not much you can do about the positions table at the beginning, but you could definitely use a for loop in the while loop to make the code shorter, like:

local currentPosition,increasing = 0,true
while true do
     if increasing then
          currentPosition += 1
     else
          currentPosition -= 1
     end
     if not positions[currentPosition] then
           if increasing then
                currentPosition -= 2
                increasing = false
           else
                currentPosition += 2
                increasing = true
           end
     end
     local desiredPosition = positions[currentPosition]
     TweenService:Create(part,tweenInfo0,{
          Position = desiredPosition,
     }):Play()
     task.wait(tweenInfo0.Time)
end

I THINK that will work but I didn’t test it myself

2 Likes

You can put the positions in a moduleScript

local TweenService: TweenService = game:GetService("TweenService")
local part: Part = script.Parent
local positions = require(script.Positions)

local tweenInfo0: TweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut,
	-1,
	true
)

for _, position in ipairs(positions) do
      local tween = TweenService:Create(part, tweenInfo0, {Position = position})
      tween:Play()
      tween.Completed:Wait()
end

The moduleScript (name it “Position” and it must be a children of your serverscript):

local positions = {
	["0"] = part.Position,
	["1"] = Vector3.new(-336.664, 6.061, -2165.951),
	["2"] = Vector3.new(-331.664, 6.061, -2135.951),
	["3"] = Vector3.new(-326.664, 6.061, -2165.951),
	["4"] = Vector3.new(-321.664, 6.061, -2135.951),
	["5"] = Vector3.new(-316.664, 6.061, -2165.951),
	["6"] = Vector3.new(-311.664, 6.061, -2135.951),
	["7"] = Vector3.new(-306.664, 6.061, -2165.951),
	["8"] = Vector3.new(-301.664, 6.061, -2135.951),
	["9"] = Vector3.new(-296.664, 6.061, -2165.951),
	["10"] = Vector3.new(-291.664, 6.061, -2135.951),
	["11"] = Vector3.new(-286.664, 6.061, -2165.951),
	["12"] = Vector3.new(-281.664, 6.061, -2135.951),
	["13"] = Vector3.new(-276.664, 6.061, -2165.951),
	["14"] = Vector3.new(-271.664, 6.061, -2135.951),
	["15"] = Vector3.new(-266.664, 6.061, -2165.951),
	["16"] = Vector3.new(-261.664, 6.061, -2135.951),
	["17"] = Vector3.new(-256.664, 6.061, -2165.951),
	["18"] = Vector3.new(-251.664, 6.061, -2135.951),
	["19"] = Vector3.new(-246.664, 6.061, -2165.951),
	["20"] = Vector3.new(-241.664, 6.061, -2135.951),
	["21"] = Vector3.new(-236.664, 6.061, -2165.951),
	["22"] = Vector3.new(-231.664, 6.061, -2135.951),
	["23"] = Vector3.new(-226.664, 6.061, -2165.951),
	["24"] = Vector3.new(-221.664, 6.061, -2135.951),
	["25"] = Vector3.new(-216.664, 6.061, -2165.951),
	["26"] = Vector3.new(-211.664, 6.061, -2135.951),
	["27"] = Vector3.new(-206.664, 6.061, -2165.951),
	["28"] = Vector3.new(-201.664, 6.061, -2135.951),
}

return positions
1 Like

Why you want to make it shorter? If you finished it, you can use it like this!

1 Like

Thought that a for i loop would do, and this seems to be working:

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

local part: Part = script.Parent

local positions = {
	["0"] = part.Position,
	["1"] = Vector3.new(-336.664, 6.061, -2165.951),
	["2"] = Vector3.new(-331.664, 6.061, -2135.951),
	["3"] = Vector3.new(-326.664, 6.061, -2165.951),
	["4"] = Vector3.new(-321.664, 6.061, -2135.951),
	["5"] = Vector3.new(-316.664, 6.061, -2165.951),
	["6"] = Vector3.new(-311.664, 6.061, -2135.951),
	["7"] = Vector3.new(-306.664, 6.061, -2165.951),
	["8"] = Vector3.new(-301.664, 6.061, -2135.951),
	["9"] = Vector3.new(-296.664, 6.061, -2165.951),
	["10"] = Vector3.new(-291.664, 6.061, -2135.951),
	["11"] = Vector3.new(-286.664, 6.061, -2165.951),
	["12"] = Vector3.new(-281.664, 6.061, -2135.951),
	["13"] = Vector3.new(-276.664, 6.061, -2165.951),
	["14"] = Vector3.new(-271.664, 6.061, -2135.951),
	["15"] = Vector3.new(-266.664, 6.061, -2165.951),
	["16"] = Vector3.new(-261.664, 6.061, -2135.951),
	["17"] = Vector3.new(-256.664, 6.061, -2165.951),
	["18"] = Vector3.new(-251.664, 6.061, -2135.951),
	["19"] = Vector3.new(-246.664, 6.061, -2165.951),
	["20"] = Vector3.new(-241.664, 6.061, -2135.951),
	["21"] = Vector3.new(-236.664, 6.061, -2165.951),
	["22"] = Vector3.new(-231.664, 6.061, -2135.951),
	["23"] = Vector3.new(-226.664, 6.061, -2165.951),
	["24"] = Vector3.new(-221.664, 6.061, -2135.951),
	["25"] = Vector3.new(-216.664, 6.061, -2165.951),
	["26"] = Vector3.new(-211.664, 6.061, -2135.951),
	["27"] = Vector3.new(-206.664, 6.061, -2165.951),
	["28"] = Vector3.new(-201.664, 6.061, -2135.951),
}

local tweenInfo: TweenInfo = TweenInfo.new(
	0.5,
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.InOut,
	-1,
	true
)

local currentPosition: number = 0
local isIncreasing = true

while true do
	for index: number = 1, 28 do
		currentPosition += 1	
		TweenService:Create(part, tweenInfo, {Position = positions[tostring(currentPosition)]}):Play()
		task.wait(tweenInfo.Time)
	end
	
	for index: number = 1, 28 do
		currentPosition -= 1	
		TweenService:Create(part, tweenInfo, {Position = positions[tostring(currentPosition)]}):Play()
		task.wait(tweenInfo.Time)
	end
end

And what it do to the part (just curiosity)?

Maybe you’d be better off going back to designing clothes if you ask this question.

  1. It moves a laser to different places and back again.

I still don’t understand :slight_smile: (for the first question)

Also I don’t only design clothes, I do lot of things! I sometimes script (but I’m not a good scripter), I create musics and more!

2 Likes

Making code shorter makes the whole thing clearer and in most cases it is also better for optimization.

1 Like

I tested this myself and it appears to look like a spring.


Pretty cool.

2 Likes