Skinstashery
Roblox
Home / Roblox / Studio script library / Shop button (RemoteEvent)
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

  1. Put two Tools named Sword and Torch in ServerStorage (or change the names in the ITEMS table).
  2. Add the server Script above to ServerScriptService. Make sure you have a script that creates leaderstats with Coins.
  3. In StarterGui, add a ScreenGui, then a TextButton inside it. Set its Text to "Buy Sword".
  4. Add the LocalScript (the second code block) inside the TextButton. Press Play and give yourself coins in the Explorer to test.

Change it

More in Player data and money

All Roblox scripts

More Roblox tools