Way to make my code more efficient?

I am new to Roblox scripting. I feel that there is a better way to do this.
My code:

while true do
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(1.5,1.5,1.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(2,2,2)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(2.5,2.5,2.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(3,3,3)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(3.5,3.5,3.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(4,4,4)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(4.5,4.5,4.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(5,5,5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(5.5,5.5,5.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(6,6,6)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(6.5,6.5,6.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(7,7,7)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(7.5,7.5,7.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(8,8,8)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(8.5,8.5,8.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(9,9,9)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(9.5,9.5,9.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(10,10,10)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(9.5,9.5,9.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(9,9,9)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(8.5,8.5,8.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(8,8,8)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(7.5,7.5,7.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(7,7,7)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(6.5,6.5,6.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(6,6,6)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(5.5,5.5,5.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(5,5,5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(4.5,4.5,4.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(4,4,4)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(3.5,3.5,3.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(3,3,3)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(2.5,2.5,2.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(2,2,2)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(1.5,1.5,1.5)
	wait(0.05)
	script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(1,1,1)
	wait(0.05)
end

you can make a tween using tweeningservice to make it smooth

You want to use a sine wave.

The logic behind it is fairly complex if you’re new so I’ll just give you the code

local function lerp(v0,v1,t)
    return v0 + (v1-v0) * t 
end

local startTime = tick()
game:GetService("RunService").Heartbeat:Connect(function()
    local sinPos = (math.sin(tick() - startTime) + 1)/2
    local newSize = lerp(1, 6, sinPos)
    script.Parent.Parent.Parent.Parent.Model.Part.Size = Vector3.new(newSize, newSize, newSize)
end)

You can make it faster by multiplying or dividing the value in math.sin, and you can make it larger by adjusting the first two parameters of the leap function

1 Like