Okay, I understand perfectly! You want the loading animation to be visually refined with the dots bouncing like a wave, all centered on any screen size, and preventing multiple clicks.
Here's the updated script that incorporates these changes. It dynamically creates the UI elements and animates the dots' position for a true bouncing effect.
lua
-- Advanced Loading Animation with Centered Bouncing Dots
-- Copy/paste this into a Script inside your button part
local button = script.Parent
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
-- ==== CONFIGURATION ==== --
local teleportTargetName = "NextRoomPart" -- Name of the part to teleport to in Workspace
local loadingTime = 2 -- Seconds the loading screen is visible
local animationSpeed = 0.3 -- Speed for slide and dot bounce animations
local dotBounceHeight = 20 -- Pixels the dots bounce up
local isClickable = true -- Boolean to prevent multiple clicks
-- ======================== --
-- Function to create the loading UI elements
local function createLoadingUI(player)
local playerGui = player:WaitForChild("PlayerGui")
-- ScreenGui acts as a container for our UI elements
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "LoadingScreen"
screenGui.ResetOnSpawn = false -- Keeps UI during respawns if necessary
screenGui.Parent = playerGui
-- Black background frame that covers the entire screen
local blackFrame = Instance.new("Frame")
blackFrame.Name = "BlackFrame"
blackFrame.Size = UDim2.new(1, 0, 1, 0) -- Full screen size
blackFrame.Position = UDim2.new(0, 0, -1, 0) -- Starts off-screen above
blackFrame.BackgroundColor3 = Color3.fromRGB(0, 0, 0) -- Black color
blackFrame.BorderSizePixel = 0 -- No border
blackFrame.Parent = screenGui
-- Container for the "Loading" text and bouncing dots, this will be centered
local loadingContainer = Instance.new("Frame")
loadingContainer.Size = UDim2.new(0, 350, 0, 120) -- Size of the text/dots area
-- Center the container: Position uses Scale for screen percentage and Offset for pixel adjustment
loadingContainer.Position = UDim2.new(0.5, -175, 0.5, -60) -- Centers 350x120 frame
loadingContainer.BackgroundTransparency = 1 -- Make container invisible
loadingContainer.Parent = blackFrame
-- "Loading" Text Label
local loadingText = Instance.new("TextLabel")
loadingText.Name = "LoadingText"
loadingText.Size = UDim2.new(1, 0, 0.65, 0) -- Takes 65% of the container's height
loadingText.Position = UDim2.new(0, 0, 0, 0) -- Positioned at the top of the container
loadingText.BackgroundTransparency = 1
loadingText.Text = "Loading"
loadingText.TextColor3 = Color3.fromRGB(255, 255, 255) -- White text
loadingText.TextScaled = true -- Scales text to fit
loadingText.Font = Enum.Font.SourceSansBold
loadingText.Parent = loadingContainer
-- Bouncing Dots Label
local dots = Instance.new("TextLabel")
dots.Name = "Dots"
dots.Size = UDim2.new(1, 0, 0.35, 0) -- Takes 35% of the container's height
dots.Position = UDim2.new(0, 0, 0.65, 0) -- Positioned right below the loadingText
dots.BackgroundTransparency = 1
dots.Text = "..." -- Initial text for dots
dots.TextColor3 = Color3.fromRGB(255, 255, 255) -- White text
dots.TextScaled = true
dots.Font = Enum.Font.SourceSansBold
dots.Parent = loadingContainer
return blackFrame, loadingText, dots, screenGui
end
-- Function to handle the player's teleportation
local function handleTeleport(player)
-- Find the target part in the Workspace
local targetPart = workspace:FindFirstChild(teleportTargetName)
-- Validate the target part
if not targetPart then
warn("⚠️ Target part '"..teleportTargetName.."' not found in Workspace!")
return
end
if not targetPart:IsA("BasePart") then
warn("⚠️ '"..teleportTargetName.."' is not a BasePart (like a Part/Cube). Cannot teleport.")
return
end
-- Get the player's character and its HumanoidRootPart
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
-- Teleport the player to the target part's position and orientation
character.HumanoidRootPart.CFrame = targetPart.CFrame
else
warn("Player character or HumanoidRootPart not found for teleport.")
end
end
-- Main function to show the loading animation and handle the sequence
local function showLoadingAnimation(player)
-- Create the UI elements for this player
local blackFrame, loadingText, dots, screenGui = createLoadingUI(player)
-- Define TweenInfo for animations
local tweenInfoSlide = TweenInfo.new(animationSpeed, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local tweenInfoBounce = TweenInfo.new(animationSpeed, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- Sine for wave-like motion
-- Slide in the black frame animation
local slideInTween = TweenService:Create(blackFrame, tweenInfoSlide, {
Position = UDim2.new(0, 0, 0, 0) -- Slide down to the top of the screen
})
slideInTween:Play()
-- Animation sequence for the bouncing dots
local dotSequenceIndex = 1 -- Controls which dot string to show (., .., ...)
local isAnimating = true -- Flag to keep the animation loop running
-- Create the individual tweens for moving dots up and down
local initialDotPosition = UDim2.new(0, 0, 0.65, 0) -- The dots' default position in the container
local bounceUpPosition = UDim2.new(0, 0, 0.65, -dotBounceHeight) -- Dots moved up by bounceHeight pixels
local bounceUpTween = TweenService:Create(dots, tweenInfoBounce, {Position = bounceUpPosition})
local bounceDownTween = TweenService:Create(dots, tweenInfoBounce, {Position = initialDotPosition})
-- Use a coroutine to run the animation loop concurrently
coroutine.wrap(function()
while isAnimating do
-- Cycle through the dot text: ".", "..", "..."
dots.Text = string.rep(".", dotSequenceIndex)
dotSequenceIndex = (dotSequenceIndex % 3) + 1 -- Cycle from 1 to 3
-- Play the bounce up animation
bounceUpTween:Play()
bounceUpTween.Completed:Wait() -- Wait for it to finish moving up
-- Play the bounce down animation
bounceDownTween:Play()
bounceDownTween.Completed:Wait() -- Wait for it to finish moving down
-- Optional: add a small delay between bounce cycles if it feels too rapid
-- wait(animationSpeed * 0.5)
end
-- After animation stops, reset dots position and clear text
dots.Position = initialDotPosition
dots.Text = ""
end)()
-- Wait for the specified loading duration
wait(loadingTime)
-- Stop the animation loop
isAnimating = false
-- Teleport the player to the next room
handleTeleport(player)
-- Slide out the black frame animation
local slideOutTween = TweenService:Create(blackFrame, tweenInfoSlide, {
Position = UDim2.new(0, 0, 1, 0) -- Slide up off-screen
})
slideOutTween:Play()
-- Clean up the UI elements once the slide-out animation is complete
slideOutTween.Completed:Connect(function()
screenGui:Destroy()
isClickable = true -- Re-enable the button for further use
end)
end
-- Add a ClickDetector to the button part
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = button
-- Connect the MouseClick event to our animation function
clickDetector.MouseClick:Connect(function(player)
-- Only proceed if the button is currently clickable
if isClickable then
isClickable = false -- Immediately disable the button to prevent multiple clicks
showLoadingAnimation(player)
end
end)
What's New and Improved:
Proper Centering for All Devices:
The loadingContainer (which holds the text and dots) is created with UDim2.new(0.5, -175, 0.5, -60).
0.5 (Scale) means 50% of the screen width/height.
-175 and -60 (Offset) are half the width and height of the loadingContainer itself, ensuring it's perfectly centered regardless of screen size.
Bouncing Dots with Wave Motion:
Instead of changing the text of the dots, we now animate the Position property of the dots TextLabel.
bounceUpPosition = UDim2.new(0, 0, 0.65, -dotBounceHeight) moves the dots up by dotBounceHeight pixels (using the Offset part of UDim2).
initialDotPosition = UDim2.new(0, 0, 0.65, 0) is the dots' default position below the "Loading" text.
Two tweens (bounceUpTween, bounceDownTween) are created and played in sequence within a coroutine. This creates a smooth up-and-down bouncing effect.
Enum.EasingStyle.Sine is used for a more natural, wave-like acceleration and deceleration.
The dots.Text now cycles between ".", "..", "..." while the bouncing happens.
Click-Only Functionality:
The script now relies solely on the ClickDetector's MouseClick event.
The isClickable boolean is set to false immediately upon clicking and only reset to true after the entire loading animation and teleport are finished.
How to Use:
Button Part: Make sure your button part in the game is named exactly TeleportButton. (I've kept the name in the script consistent with the logic). If you want to name the part something else, you'll need to change local button = script.Parent to local button = script.Parent.Parent.YourButtonPartName if the script is in a child of the button, or just local button = game.Workspace.YourButtonPartName if the script is a separate script in ServerScriptService. The simplest is to put the script directly inside the button part.
Target Part: Create a part in Workspace and name it exactly NextRoomPart. Position it where you want players to be teleported.
Copy-Paste: Place this entire script inside your button part.
Now, when a player clicks the button, they'll see a perfectly centered loading screen with elegantly bouncing dots, and then teleport! Let me know if you want any more tweaks!