Below is an example of how you can set up a Roblox currency system that displays a GUI animation each time a player gains currency. This example uses a server script to manage currency (via a simple leaderstats system), a RemoteEvent to notify the client when the currency changes, and a local script with TweenService to animate a GUI element.
1. Setup the RemoteEvent
Place this RemoteEvent in ReplicatedStorage (or create it in the code):
ReplicatedStorage > CurrencyEvent
(If you want to create it by code, see the server script below.)
2. Server-Side Script
Place the following script into ServerScriptService (for example, name it CurrencyManager). This script creates a leaderstats folder with a Currency value for each player. When the currency value changes, it fires a RemoteEvent to the respective player.
lua
--[[
Server Script: CurrencyManager
Place this script in ServerScriptService
--]]
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Create or get the RemoteEvent (if it doesn't exist, create it)
local currencyEvent = ReplicatedStorage:FindFirstChild("CurrencyEvent")
if not currencyEvent then
currencyEvent = Instance.new("RemoteEvent")
currencyEvent.Name = "CurrencyEvent"
currencyEvent.Parent = ReplicatedStorage
end
-- Function to create a leaderstats folder with a Currency value for each player
local function onPlayerAdded(player)
-- Create leaderstats folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create a Currency value
local currency = Instance.new("IntValue")
currency.Name = "Currency"
currency.Parent = leaderstats
currency.Value = 0 -- starting currency
-- Listen for changes on the currency value
currency.Changed:Connect(function(newValue)
-- Fire the client event to show an animation when currency changes
currencyEvent:FireClient(player, newValue)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
3. Client-Side GUI and Animation
Create the GUI:
In StarterGui, add a ScreenGui (name it e.g. CurrencyGui).
Inside the ScreenGui, add a Frame (name it CurrencyFrame) or you can work directly with a TextLabel if you prefer. For this example, we’ll use a TextLabel.
Inside the CurrencyFrame, add a TextLabel (name it CurrencyText). Set its properties as needed (for example, center it on the screen, set a nice font, and make sure its Visible property is set to false by default).
Tip: Adjust the TextLabel’s AnchorPoint, Position, Size, BackgroundTransparency, etc., so that the animation looks good.
Local Script for Animation:
Place the following LocalScript inside the CurrencyGui (or within the TextLabel if you prefer). This script listens for the CurrencyEvent and then uses TweenService to animate the TextLabel.
lua
--[[
Local Script: CurrencyAnimation
Place this in StarterGui > CurrencyGui
--]]
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
-- Wait for the RemoteEvent you created
local currencyEvent = ReplicatedStorage:WaitForChild("CurrencyEvent")
-- Reference to the GUI elements (adjust the path based on your hierarchy)
local screenGui = script.Parent
local currencyText = screenGui:WaitForChild("CurrencyText")
-- Function to play the animation when currency is gained
local function playCurrencyAnimation(newValue)
-- Update the text to show how much currency has been gained
currencyText.Text = "+ Currency: " .. tostring(newValue)
currencyText.Visible = true
-- Reset properties for the animation
-- Starting near the center of the screen (you can customize these values)
currencyText.Position = UDim2.new(0.5, 0, 0.5, 0)
currencyText.TextTransparency = 0
currencyText.TextStrokeTransparency = 0
-- Define the end position for the animation (moves upward)
local endPosition = UDim2.new(0.5, 0, 0.2, 0)
-- Tween information: duration, easing style, and direction
local tweenInfo = TweenInfo.new(
1, -- duration in seconds
Enum.EasingStyle.Back,
Enum.EasingDirection.Out
)
-- Tween properties: moving upward and fading out the text
local tweenProperties = {
Position = endPosition,
TextTransparency = 1,
TextStrokeTransparency = 1
}
-- Create and play the tween
local tween = TweenService:Create(currencyText, tweenInfo, tweenProperties)
tween:Play()
-- After the tween completes, hide the text until next time
tween.Completed:Connect(function()
currencyText.Visible = false
end)
end
-- Listen to the RemoteEvent fired from the server
currencyEvent.OnClientEvent:Connect(playCurrencyAnimation)
4. Testing the System
Run the game in Roblox Studio.
Open the Output window to check for any errors.
When a player's currency changes on the server (for example, if you manually change the Currency value during testing or add code for currency gains), the RemoteEvent will fire.
The local script will capture that event and animate the CurrencyText with a Tween that moves it upward and fades it out.
You can further customize the GUI elements, the text content, and the tween properties (like duration, easing style, and target positions) to fit your game’s style.
This basic system can be expanded—for example, you might want to show only the difference in currency (i.e., by calculating how much was added) or stack multiple animations if currency is gained in rapid succession. Enjoy building your Roblox game!