RB-004
Roblox shop button (remoteevent) script
A buy button where the server checks the price and coins, so players cannot cheat.
- Script type
- Script
- Where it goes
- ServerScriptService
- Category
- Player data and money
For your own game in Roblox Studio. Not for use in other people's games.
The code
Server script · Script · ServerScriptService
-- Shop (server side). The server decides if a purchase is allowed.
-- The client only says WHICH item it wants. It never says the price.
-- Needs the Leaderstats (coins) or Save data script so player.leaderstats.Coins exists.
local ITEMS = {
-- [item name] = { price = coins, tool = name of a Tool in ServerStorage }
Sword = { price = 50, tool = "Sword" },
Torch = { price = 20, tool = "Torch" },
}
local BUY_COOLDOWN = 0.5 -- seconds between purchases for one player
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
-- Make the RemoteEvent if it is not there yet.
local buyEvent = ReplicatedStorage:FindFirstChild("BuyItem")
if not buyEvent then
buyEvent = Instance.new("RemoteEvent")
buyEvent.Name = "BuyItem"
buyEvent.Parent = ReplicatedStorage
end
local lastBuy = {}
buyEvent.OnServerEvent:Connect(function(player, itemName)
-- 1. Check what the client sent. Never trust it.
if type(itemName) ~= "string" then
return
end
local item = ITEMS[itemName]
if not item then
return
end
-- 2. Slow down spam.
local now = os.clock()
if lastBuy[player] and now - lastBuy[player] < BUY_COOLDOWN then
return
end
lastBuy[player] = now
-- 3. Check the player can pay.
local leaderstats = player:FindFirstChild("leaderstats")
local coins = leaderstats and leaderstats:FindFirstChild("Coins")
if not coins or coins.Value < item.price then
return
end
-- 4. Check the tool exists and the player has a Backpack.
local template = ServerStorage:FindFirstChild(item.tool)
local backpack = player:FindFirstChildOfClass("Backpack")
if not template or not backpack then
return
end
-- 5. Take the coins, give the item.
coins.Value -= item.price
template:Clone().Parent = backpack
end)
Players.PlayerRemoving:Connect(function(player)
lastBuy[player] = nil
end)Button script · LocalScript · Inside the TextButton (StarterGui > ScreenGui > TextButton)
-- Shop button (client side). Sends the item name to the server when clicked.
local ITEM_NAME = "Sword" -- must match a name in the ITEMS table on the server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local button = script.Parent
local buyEvent = ReplicatedStorage:WaitForChild("BuyItem")
button.Activated:Connect(function()
buyEvent:FireServer(ITEM_NAME)
end)How to set it up
- Put two Tools named Sword and Torch in ServerStorage (or change the names in the ITEMS table).
- Add the server Script above to ServerScriptService. Make sure you have a script that creates leaderstats with Coins.
- In StarterGui, add a ScreenGui, then a TextButton inside it. Set its Text to "Buy Sword".
- Add the LocalScript (the second code block) inside the TextButton. Press Play and give yourself coins in the Explorer to test.
Change it
- The price lives only on the server. A cheater can fire the event, but all they can do is ask for an item. The server still takes the coins.
- For a second button, copy the TextButton and change ITEM_NAME in its LocalScript.
- Anything that gives coins, items or levels should be checked on the server like this.
- This sends no reply to the player. To show "Not enough coins", add a second RemoteEvent that the server fires back.
More in Player data and money
More Roblox tools
- Shirt and pants templatesFree Roblox clothing templates to download: hoodies, jerseys, jeans, joggers and more.
- Robux tax calculatorWork out what you receive after the marketplace fee, or what to charge.
- Robux to USD calculatorConvert earned Robux to dollars at a DevEx rate you can edit.
- Shirt and pants template makerColour each panel of the 585×559 clothing template and download it.
- Username generatorGenerate usernames that fit the 3–20 character rule.