How can I make it so you have to pay in-game currency to use it?

So I have this script to give you a random item. However, I am trying to figure out how to make it so it takes away Coins from the player, and if the player doesn’t have enough Coins, it wont work. Any help is greatly appreciated!

local part = script.Parent
local prompt = part:WaitForChild("ProximityPrompt")
local RS = game:GetService("ReplicatedStorage")
local itemsFolder = RS:WaitForChild("Random")
local minTime = 1
local maxTime = 5 

local function giveRandomItem(player)
	local items = itemsFolder:GetChildren()
	if #items > 0 then
		local randomItem = items[math.random(1, #items)]:Clone()
		randomItem.Parent = player.Backpack
	end
end

local function handlePrompt()
	prompt.Enabled = false
	wait(math.random(minTime, maxTime))
	prompt.Enabled = true
end

prompt.Triggered:Connect(function(player)
	giveRandomItem(player)
	handlePrompt()
end)

Hello, you can modify your script so that the player needs to pay a specific amount of coins before receiving an item. Here’s an adjusted version of your code:

local part = script.Parent
local prompt = part:WaitForChild("ProximityPrompt")
local RS = game:GetService("ReplicatedStorage")
local itemsFolder = RS:WaitForChild("Random")

local minTime = 1
local maxTime = 5
local itemCost = 10 

-- Function to get the player's coins
local function getPlayerCoins(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
        local coins = leaderstats:FindFirstChild("Coins")
        if coins then
            return coins
        end
    end
    return nil
end

local function giveRandomItem(player)
    local coins = getPlayerCoins(player)
    
    if coins and coins.Value >= itemCost then
        local items = itemsFolder:GetChildren()
        if #items > 0 then
            local randomItem = items[math.random(1, #items)]:Clone()
            randomItem.Parent = player.Backpack
            coins.Value = coins.Value - itemCost -- Deduct coins
        end
    else
        warn(player.Name .. " does not have enough coins.")
    end
end

local function handlePrompt()
    prompt.Enabled = false
    wait(math.random(minTime, maxTime))
    prompt.Enabled = true
end

prompt.Triggered:Connect(function(player)
    giveRandomItem(player)
    handlePrompt()
end)

With this change, the script checks if the player has enough coins before giving them an item. If they have the required amount, the cost is deducted, and they receive a random item. If they do not have enough coins, they will not receive anything.

Make sure your game includes a "Coins" value inside "leaderstats" for each player, as this script relies on it. I hope it helps you!.

Thank you so so much. This helps a lot. I try my best to understand the code everytime I recieve help. Thank you again!