SpringService | A Roblox UI Spring Animation Solution


SpringService is a Roblox UI Spring system designed for ease-of-use and performance while maintaining a high level of customizability and features.

GitHub | Creator Store | Patreon | Test Place

Welcome to SpringService

Roblox has had a problem for a few years now - UI animation. TweenService is great, but there’s only so much you can do with it. Most modern UI involves what is considered “spring motion” now, which is pretty much a simple way of describing the type of motion the animation behaves.

This system has a lot of specific details that I will cover briefly here, but the GitHub has all of the documentation and API on it with deeper descriptions into the math behind this system and how specific values are calculated, etc.

How it works

SpringService has a .new() constructor and 4 methods:

  • :Play()
  • :SetTarget()
  • :Stop()
  • :Destroy()

.new()

The constructor takes 4 parameters to construct the Spring object:

  • instance: Instance
  • response: number
  • damping: number

The instance parameter is the Instance that is getting animated.

The response parameter is how “quick” the Spring feels. Higher values = slower feel

The damping parameter is how “smooth” the Spring feels. Higher values = smoother feel

:Play()

Plays the Spring animation

:SetTarget()

Sets a new target for the Spring. If this is set during the Spring’s motion, the Spring will smoothly redirect it’s path to the new target mid-motion. If called after the Spring has finished it’s animation, the :Play() method will need to be called again.

:Stop()

Stops the Spring animation at its current point immediately. This will be a very abrupt stop.

:Destroy()

Destroys the spring object.


All of the above properties and methods are described in much more detail on the GitHub, as well as all of the additional properties of the Spring object.

Basic example:

“Spring” between 2 targets

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Spring = require(ReplicatedStorage.Spring)

local frame = script.Parent
local response = 1.5 -- Somewhat quick
local damping = .85 -- Slightly bouncy

local startPosition = UDim2.new(.2,0,.5,0)
local endPosition = UDim2.new(.8,0,.5,0)

local atStart = false

local newSpring = Spring.new(frame, response, damping)

newSpring.Finished:Connect(function()	
	if not atStart then
		newSpring:SetTarget({Position = startPosition})
		atStart = true
	else
		newSpring:SetTarget({Position = endPosition})
		atStart = false
	end
end)

newSpring:Play()

Video of example:

Another example:

Change the target part-way through the animation. For this example I will use a draggable object that will snap back to it’s original position.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local Spring = require(ReplicatedStorage:WaitForChild("Spring"))

local gui = script.Parent.Parent
local frame = script.Parent

local response = .7 -- Quicker frequency
local damping = .9 -- Slight bounce

local startPosition = UDim2.new(.5,0,.5,0)

local newSpring = Spring.new(frame, response, damping)

local connection = nil
local hovering = false

frame.MouseEnter:Connect(function()
	hovering = true
end)

frame.MouseLeave:Connect(function()
	hovering = false
end)

local function ConvertToScaleUDim2(x, y, xabs, yabs)
	return UDim2.new(x/xabs, 0, y/yabs, 0)
end

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and hovering then				
		connection = RunService.RenderStepped:Connect(function(dt)
			local mouse = UserInputService:GetMouseLocation()
			
			local position = ConvertToScaleUDim2(
				mouse.X,
				mouse.Y - 40, -- 40 is the height of the top bar 
				gui.AbsoluteSize.X,
				gui.AbsoluteSize.Y
			)
						
			newSpring:SetTarget({Position = position})
		end)
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then		
		if connection then
			connection:Disconnect()
			connection = nil
		end
										
		newSpring:SetTarget({Position = startPosition})		
	end
end)

Video of example:

If you would to test out the system, you can visit the test experience.


Thank you for using SpringService

If you have any questions about the system, feel free to ask. You can get SpringService from the GitHub, the Creator Store, or on our Patreon if you’d like to support us. (Note: this module is 100% free to use, purchasing it through our Patreon is purely for those who would like to support us, which we thank you for in advance)

7 Likes