Okay, let's create a core Roblox game system based on the concept of Taproad.io – a clicker/idle game where you tap to earn currency, then use that currency to upgrade your tapping power and idle income.
This will include:
- Core Clicker Mechanic: Tap a button to earn "Coins".
- Upgrades System:
- Upgrade "Click Power" (earns more coins per tap).
- Upgrade "Idle Power" (earns coins automatically over time).
- Data Saving: Player data (coins, upgrade levels) will save and load using DataStoreService.
- Basic UI: Display coins, click button, upgrade buttons.
- Idle Income: Players earn coins based on their idle power even when offline.
Game Structure and Setup in Roblox Studio
Before adding code, set up the necessary folders and UI elements:
-
In ReplicatedStorage:
- Create a
Folder named Remotes.
- Inside
Remotes, create a RemoteEvent named ClickEvent.
- Inside
Remotes, create a RemoteEvent named BuyUpgradeEvent.
-
In ServerScriptService:
- Create a
Script named GameManager.
-
In StarterPlayer/StarterPlayerScripts:
- Create a
LocalScript named PlayerUIController.
-
In StarterGui:
- Create a
ScreenGui named MainGameUI.
- Inside
MainGameUI:
- Add a
Frame named CoinsFrame (top-left, for example).
- Inside
CoinsFrame, add a TextLabel named CoinsAmount.
- Set its
Text to "Coins: 0" and TextScaled to true.
- Add a
TextButton named ClickButton (center of the screen).
- Set its
Text to "TAP!" and TextScaled to true.
- Set its
Size to UDim2.new(0.2, 0, 0.1, 0) and Position to UDim2.new(0.5, -50, 0.5, -20).
- Add a
Frame named UpgradesFrame (e.g., bottom of the screen).
- Set its
BackgroundColor3 to a light color (e.g., 170, 170, 170).
- Set
BorderSizePixel to 0.
- Add a
UIListLayout inside UpgradesFrame to organize buttons vertically.
- Inside
UpgradesFrame:
- Add a
TextButton named ClickPowerUpgrade.
- Set
Text to "Upgrade Click Power" and TextScaled to true.
- Set
Size to UDim2.new(1, 0, 0.3, 0).
- Add a
TextButton named IdlePowerUpgrade.
- Set
Text to "Upgrade Idle Power" and TextScaled to true.
- Set
Size to UDim2.new(1, 0, 0.3, 0).
1. ServerScriptService/GameManager.lua
This script handles all the core game logic, data management, and server-side validation.
-- Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local MarketplaceService = game:GetService("MarketplaceService") -- Future expansion
-- DataStore
local playerDataStore = DataStoreService:GetDataStore("TaproadPlayerCoinsAndUpgradesV2") -- V2 for potential schema changes
-- Remotes
local Remotes = ServerStorage:WaitForChild("Remotes")
local ClickEvent = Remotes:WaitForChild("ClickEvent")
local BuyUpgradeEvent = Remotes:WaitForChild("BuyUpgradeEvent")
-- Configuration for Upgrades
-- BaseCost: Cost for Level 0 (first purchase)
-- CostMultiplier: How fast the cost increases per level
-- EffectPerLevel: How much the stat increases per level
local UPGRADES_CONFIG = {
ClickPower = {
BaseCost = 10,
CostMultiplier = 1.5, -- Cost = BaseCost * (CostMultiplier ^ currentLevel)
EffectPerLevel = 1 -- Each level adds 1 to click gain
},
IdlePower = {
BaseCost = 50,
CostMultiplier = 1.8,
EffectPerLevel = 1 -- Each level adds 1 coin per second (when online)
}
}
-- Function to calculate upgrade cost
local function getUpgradeCost(upgradeType, currentLevel)
local config = UPGRADES_CONFIG[upgradeType]
if not config then return math.huge end -- Return a very high number if type invalid
local cost = config.BaseCost * (config.CostMultiplier ^ currentLevel)
return math.floor(cost) -- Ensure cost is a whole number
end
-- Function to initialize/load player data
local function getPlayerData(player)
local success, data = pcall(function()
return playerDataStore:GetAsync(player.UserId)
end)
if success and data then
-- Backward compatibility or default values if new fields are added
data.Coins = data.Coins or 0
data.ClickPowerLevel = data.ClickPowerLevel or 1
data.IdlePowerLevel = data.IdlePowerLevel or 0
data.LastLogin = data.LastLogin or os.time() -- Record last login for idle gain
return data
else
warn("Error loading data for " .. player.Name .. ": " .. tostring(data))
-- Return default data for new players or on load error
return {
Coins = 0,
ClickPowerLevel = 1,
IdlePowerLevel = 0,
LastLogin = os.time()
}
end
end
-- Function to save player data
local function savePlayerData(player)
local data = player:GetAttribute("TaproadData") -- Get the table from attribute
if data then
-- Update data table from current value objects
data.Coins = player.PlayerData.Coins.Value
data.ClickPowerLevel = player.PlayerData.ClickPowerLevel.Value
data.IdlePowerLevel = player.PlayerData.IdlePowerLevel.Value
data.LastLogin = os.time() -- Update last login time before saving
local success, err = pcall(function()
playerDataStore:SetAsync(player.UserId, data)
end)
if not success then
warn("Failed to save data for " .. player.Name .. ": " .. err)
end
end
end
-- Player Added Event
Players.PlayerAdded:Connect(function(player)
local data = getPlayerData(player)
-- Create a folder in player for storing data values, easily replicated to client
local playerFolder = Instance.new("Folder")
playerFolder.Name = "PlayerData"
playerFolder.Parent = player
local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Value = data.Coins
coins.Parent = playerFolder
local clickPower = Instance.new("NumberValue")
clickPower.Name = "ClickPowerLevel"
clickPower.Value = data.ClickPowerLevel
clickPower.Parent = playerFolder
local idlePower = Instance.new("NumberValue")
idlePower.Name = "IdlePowerLevel"
idlePower.Value = data.IdlePowerLevel
idlePower.Parent = playerFolder
-- Store the actual data table as an attribute for saving
player:SetAttribute("TaproadData", data)
-- Calculate and apply offline idle income
local lastLoginTime = data.LastLogin
local currentTime = os.time()
local timePassed = currentTime - lastLoginTime
if timePassed > 0 and idlePower.Value > 0 then
local idleGain = timePassed * (idlePower.Value * UPGRADES_CONFIG.IdlePower.EffectPerLevel)
coins.Value += idleGain
ClickEvent:FireClient(player, "IdleGain", idleGain) -- Notify client of offline gain
print(player.Name .. " gained " .. math.floor(idleGain) .. " coins while offline.")
end
-- Start continuous idle income update (while player is online)
-- This uses a coroutine to not block other events
task.spawn(function()
while task.wait(1) do -- Update every second
if player and player.Parent and idlePower.Value > 0 then
local gain = idlePower.Value * UPGRADES_CONFIG.IdlePower.EffectPerLevel
coins.Value += gain
ClickEvent:FireClient(player, "CoinGain", gain) -- Send individual tick gains
else
break -- Player left or idle power is 0, stop loop
end
end
end)
end)
-- Player Removing Event (saving data)
Players.PlayerRemoving:Connect(savePlayerData)
-- BindToClose (saving data when server shuts down)
game:BindToClose(function()
print("Server shutting down, saving all player data...")
for _, player in pairs(Players:GetPlayers()) do
savePlayerData(player)
end
print("All player data saved.")
-- Wait a bit to ensure data stores finish
task.wait(2)
end)
-- RemoteEvent: Click Event (Client -> Server)
ClickEvent.OnServerEvent:Connect(function(player, action)
if action == "Click" then
local coinsValue = player.PlayerData.Coins
local clickPowerLevel = player.PlayerData.ClickPowerLevel
local gain = clickPowerLevel.Value * UPGRADES_CONFIG.ClickPower.EffectPerLevel
coinsValue.Value += gain
ClickEvent:FireClient(player, "CoinGain", gain) -- Notify client for visual feedback
end
end)
-- RemoteEvent: Buy Upgrade Event (Client -> Server)
BuyUpgradeEvent.OnServerEvent:Connect(function(player, upgradeType)
local coinsValue = player.PlayerData.Coins
local clickPowerLevel = player.PlayerData.ClickPowerLevel
local idlePowerLevel = player.PlayerData.IdlePowerLevel
local currentLevel
local targetValue
if upgradeType == "ClickPower" then
currentLevel = clickPowerLevel.Value
targetValue = clickPowerLevel
elseif upgradeType == "IdlePower" then
currentLevel = idlePowerLevel.Value
targetValue = idlePowerLevel
else
BuyUpgradeEvent:FireClient(player, "UpgradeFailed", upgradeType, "Invalid upgrade type!")
warn(player.Name .. " tried to buy invalid upgrade: " .. upgradeType)
return
end
local cost = getUpgradeCost(upgradeType, currentLevel)
if coinsValue.Value >= cost then
coinsValue.Value -= cost
targetValue.Value += 1 -- Increment level
BuyUpgradeEvent:FireClient(player, "UpgradeSuccess", upgradeType, targetValue.Value, cost)
print(player.Name .. " upgraded " .. upgradeType .. " to level " .. targetValue.Value .. " for " .. cost .. " coins.")
else
BuyUpgradeEvent:FireClient(player, "UpgradeFailed", upgradeType, "Not enough coins!")
end
end)
print("GameManager loaded.")
2. StarterPlayer/StarterPlayerScripts/PlayerUIController.lua
This script manages the client-side UI updates and sends player actions to the server.
-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService") -- For floating numbers
-- Player
local Player = Players.LocalPlayer
-- UI References
local PlayerGui = Player:WaitForChild("PlayerGui")
local MainGameUI = PlayerGui:WaitForChild("MainGameUI")
local CoinsAmountText = MainGameUI:WaitForChild("CoinsFrame"):WaitForChild("CoinsAmount")
local ClickButton = MainGameUI:WaitForChild("ClickButton")
local UpgradesFrame = MainGameUI:WaitForChild("UpgradesFrame")
local ClickPowerUpgradeButton = UpgradesFrame:WaitForChild("ClickPowerUpgrade")
local IdlePowerUpgradeButton = UpgradesFrame:WaitForChild("IdlePowerUpgrade")
-- Remote Events
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local ClickEvent = Remotes:WaitForChild("ClickEvent")
local BuyUpgradeEvent = Remotes:WaitForChild("BuyUpgradeEvent")
-- Player Data Values (will be replicated from server)
local PlayerData = Player:WaitForChild("PlayerData")
local PlayerCoins = PlayerData:WaitForChild("Coins")
local PlayerClickPower = PlayerData:WaitForChild("ClickPowerLevel")
local PlayerIdlePower = PlayerData:WaitForChild("IdlePowerLevel")
-- Configuration for Upgrades (Client-side mirror for display costs)
local UPGRADES_CLIENT_CONFIG = {
ClickPower = {
BaseCost = 10,
CostMultiplier = 1.5,
EffectPerLevel = 1
},
IdlePower = {
BaseCost = 50,
CostMultiplier = 1.8,
EffectPerLevel = 1
}
}
-- Function to calculate upgrade cost (client-side for display)
local function getUpgradeCostClient(upgradeType, currentLevel)
local config = UPGRADES_CLIENT_CONFIG[upgradeType]
if not config then return math.huge end
local cost = config.BaseCost * (config.CostMultiplier ^ currentLevel)
return math.floor(cost)
end
-- Function to update all UI elements
local function updateUI()
CoinsAmountText.Text = "Coins: " .. math.floor(PlayerCoins.Value)
-- Update Click Power Upgrade Button
local nextClickCost = getUpgradeCostClient("ClickPower", PlayerClickPower.Value)
ClickPowerUpgradeButton.Text = string.format("Upgrade Click Power\nLevel: %d\nCost: %d Coins", PlayerClickPower.Value, nextClickCost)
ClickPowerUpgradeButton.TextColor3 = (PlayerCoins.Value >= nextClickCost) and Color3.fromRGB(0, 255, 0) or Color3.fromRGB(255, 0, 0)
-- Update Idle Power Upgrade Button
local nextIdleCost = getUpgradeCostClient("IdlePower", PlayerIdlePower.Value)
IdlePowerUpgradeButton.Text = string.format("Upgrade Idle Power\nLevel: %d\nCost: %d Coins", PlayerIdlePower.Value, nextIdleCost)
IdlePowerUpgradeButton.TextColor3 = (PlayerCoins.Value >= nextIdleCost) and Color3.fromRGB(0, 255, 0) or Color3.fromRGB(255, 0, 0)
end
-- Connect UI updates to value changes
PlayerCoins.Changed:Connect(updateUI)
PlayerClickPower.Changed:Connect(updateUI)
PlayerIdlePower.Changed:Connect(updateUI)
-- Initial UI update once player data is loaded
Player.CharacterAppearanceLoaded:Wait() -- Wait for player to fully load
updateUI()
-- Click Button Event
ClickButton.MouseButton1Click:Connect(function()
ClickEvent:FireServer("Click")
end)
-- Upgrade Buttons Events
ClickPowerUpgradeButton.MouseButton1Click:Connect(function()
BuyUpgradeEvent:FireServer("ClickPower")
end)
IdlePowerUpgradeButton.MouseButton1Click:Connect(function()
BuyUpgradeEvent:FireServer("IdlePower")
end)
-- RemoteEvent: ClickEvent (Server -> Client)
ClickEvent.OnClientEvent:Connect(function(action, value, optionalMsg)
if action == "CoinGain" then
-- Create a floating text effect for coin gain
local textLabel = Instance.new("TextLabel")
textLabel.Text = "+" .. math.floor(value)
textLabel.BackgroundTransparency = 1
textLabel.TextColor3 = Color3.fromRGB(0, 255, 0) -- Green
textLabel.Font = Enum.Font.SourceSansBold
textLabel.TextScaled = true
textLabel.Size = UDim2.new(0, 100, 0, 30)
textLabel.Position = ClickButton.Position + UDim2.new(0, 0, -0.05, 0) -- Slightly above the click button
textLabel.Parent = MainGameUI
-- Tween for movement and fade
local tweenInfo = TweenInfo.new(1.2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local goal = {
Position = textLabel.Position + UDim2.new(0, 0, -0.1, 0), -- Move up
TextTransparency = 1 -- Fade out
}
local tween = TweenService:Create(textLabel, tweenInfo, goal)
tween:Play()
tween.Completed:Connect(function()
textLabel:Destroy()
end)
elseif action == "IdleGain" then
-- Display a message for offline income
local message = "Welcome back! You earned " .. math.floor(value) .. " coins while you were away!"
game:GetService("StarterGui"):SetCore("SendNotification", {
Title = "Offline Earnings!",
Text = message,
Duration = 5,
})
end
end)
-- RemoteEvent: BuyUpgradeEvent (Server -> Client)
BuyUpgradeEvent.OnClientEvent:Connect(function(status, upgradeType, levelOrMsg, cost)
if status == "UpgradeSuccess" then
-- Optional: Play a sound, show a quick "UPGRADED!" message
print("Successfully upgraded " .. upgradeType .. " to level " .. levelOrMsg .. " for " .. cost .. " coins!")
elseif status == "UpgradeFailed" then
-- Optional: Show an error message to the player (e.g., "Not enough coins!")
game:GetService("StarterGui"):SetCore("SendNotification", {
Title = "Upgrade Failed!",
Text = levelOrMsg,
Duration = 3,
Icon = "rbxassetid://6207038144" -- Red X icon
})
end
end)
print("PlayerUIController loaded.")
How to Set Up and Test:
- Open Roblox Studio.
- Create a New Baseplate project.
- Follow the "Game Structure and Setup" steps above to create the folders, remote events, and UI elements exactly as described. Pay close attention to naming!
- Copy and paste the code into the respective script files (
GameManager.lua and PlayerUIController.lua).
- Run the game (File -> Play or F5).
You should see:
- A "Coins: 0" display at the top left.
- A "TAP!" button in the middle.
- Two upgrade buttons at the bottom.
- Click the "TAP!" button to earn coins.
- When you have enough coins, click the upgrade buttons. Their text color will turn green if you can afford it.
- Leave the game and rejoin after a minute or two, and you should get a notification about offline earnings (if you have Idle Power).
- Your data should persist between sessions.
This provides a solid foundation for a Roblox clicker game! You can expand upon this by adding more upgrades, prestige systems, leaderboards, visual effects, and monetization (Game Passes/Developer Products).