Any way to make this script more "efficient" or cleaner?

Honestly this script looks like a hair comb with spaghetti spilled on it. Any way to make this more neat?
FYI: this is a script to make a rolling counter like EarthBound’s rolling hp meter

Code:

zero = UDim2.new(0, 0, 0, -42 * 0)
one = UDim2.new(0, 0, 0, (-42 * 1))
two = UDim2.new(0, 0, 0, (-42 * 2))
three = UDim2.new(0, 0, 0, (-42 * 3))
four = UDim2.new(0, 0, 0, (-42 * 4))
five = UDim2.new(0, 0, 0, (-42 * 5))
six = UDim2.new(0, 0, 0, (-42 * 6))
seven = UDim2.new(0, 0, 0, (-42 * 7))
eight = UDim2.new(0, 0, 0, (-42 * 8))
nine = UDim2.new(0, 0, 0, (-42 * 9))
zer = UDim2.new(0, 0, 0, (-42 * 10))

e1 = Enum.EasingDirection.In
e2 = Enum.EasingStyle.Quad

local function setones(number, speed)
	if number == 1 then
		script.Parent.Frame:TweenPosition(one,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 2 then
		script.Parent.Frame:TweenPosition(two,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 3 then
		script.Parent.Frame:TweenPosition(three,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 4 then
		script.Parent.Frame:TweenPosition(four,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 5 then
		script.Parent.Frame:TweenPosition(five,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 6 then
		script.Parent.Frame:TweenPosition(six,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 7 then
		script.Parent.Frame:TweenPosition(seven,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 8 then
		script.Parent.Frame:TweenPosition(eight,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 9 then
		script.Parent.Frame:TweenPosition(nine,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 0 then
		script.Parent.Frame:TweenPosition(zero,e1,e2,speed,true)
		wait(speed + 0.1)
	elseif number == 10 then
		script.Parent.Frame:TweenPosition(zer,e1,e2,speed,true)
		wait(speed + 0.1)
		script.Parent.Frame.Position = zero
	end
end

while true do
	wait()
	for i = 0, 10 do
		setones(i, 0.2)
	end
end

This post should be in #help-and-feedback:code-review

1 Like

Okay now that this post is in the right place, there are multiple things i can point out here

  1. If you can, ALWAYS define variables with local it makes it faster to reference since it’s embedded into the script environment

  2. You don’t need an elseif chain, you already have the loop

  3. If your meant to increment the tween then why not just loop it in a table

Here’s what i think it would look like if i had a go writing it to shorten it by a bit

local Tweens = game:GetService("TweenService")

local Frame = script.Parent.Frame

local EasingStyle = Enum.EasingStyle.Quad
local EasingDir = Enum.EasingDirection.In

local function CreateTween(Goal, Speed)
   local Info = TweenInfo.new(Speed, EasingStyle, EasingDir)
   local tween = Tweens:Create(Frame, Info, Goal)
   return tween
end

while task.wait() do
   for count = 0, 10 do
      local Goal = Udim2.fromOffset(0, -42 * count)
      local tween = CreateTween({Position = Goal}, 0.2)
      tween:Play()
      tween.Completed:Wait()
   end
end
3 Likes