How to make a shop system in Roblox (with a script)
A shop needs three things: a currency, a list of items with prices, and a safe way to buy. The most common mistake is letting the client decide the price. Here is a small shop that does it correctly.
The rule that matters most
Never trust the client. The player's screen can send a message that says "I bought the sword", but only the server should check the price, subtract the coins and give the item. If the client controls any of that, exploiters will give themselves everything for free.
The script
Put this in a Script inside ServerScriptService. It creates a RemoteEvent, gives every player 500 coins on the leaderboard, and handles purchases with a 5 second cooldown.
-- ServerScriptService/ShopService
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local COOLDOWN = 5
local ITEMS = { Sword = 100, Shield = 150, Potion = 25 }
local buyEvent = Instance.new("RemoteEvent")
buyEvent.Name = "BuyItem"
buyEvent.Parent = ReplicatedStorage
local lastBuy = {}
Players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 500
coins.Parent = stats
end)
Players.PlayerRemoving:Connect(function(player)
lastBuy[player] = nil
end)
buyEvent.OnServerEvent:Connect(function(player, itemName)
if typeof(itemName) ~= "string" then return end
local price = ITEMS[itemName]
if not price then return end
local now = os.clock()
if lastBuy[player] and now - lastBuy[player] < COOLDOWN then return end
local coins = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Coins")
if not coins or coins.Value < price then return end
lastBuy[player] = now
coins.Value -= price
-- give the item here, for example clone a Tool from ServerStorage into player.Backpack
end)How it works
- ITEMS is the price list. Add items by adding lines.
- The event carries only the item name. The price always comes from the server's own table.
- The checks reject a wrong type, an unknown item, a purchase made too soon, and a player without enough coins.
- Giving the item is left as a comment, because it depends on your game. Usually you clone a Tool from ServerStorage into the player's Backpack.
Connecting a button
In a LocalScript inside your shop button, call game.ReplicatedStorage.BuyItem:FireServer("Sword") when the button is clicked. The shop window and buttons themselves are UI, which is the fiddly part.
Let Kavon build the whole thing
Doing this by hand means a server script, a UI, a LocalScript, item Tools and testing. Kavon can do all of it from one prompt, for example:
Add a shop with three items (Sword 100, Shield 150, Potion 25), coins as currency shown on the leaderboard, a shop window opened with a button, and a 5 second cooldown between purchases.
It reads your game first, creates the scripts and the interface, and playtests it. If something is off, you can ask for changes or press Ctrl+Z.