How do I make the player get money on the treasure once?

I want the player to get money after touching the treasure once and don’t get the money again after touching it.

Here is the script I made:

local debounce = true

local moneySFX = script.Parent.Money
local moneyEmitter = script.Parent.ParticleEmitter
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if debounce then
			debounce = false
			moneySFX:Play()
			moneyEmitter.Enabled = true
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Money.Value = player.leaderstats.Money.Value + 25
			wait(1)
			moneyEmitter.Enabled = false
			debounce = true -- add this line so you can get cash again
		end
	end
end)

You can put a value inside the player, and if they have that value then return the script.

If this is a server-sided script, your best option is to create a Debounces table to store every player who has touched the chest. If the player’s id is already in the table, stop them from claiming the chest again.

local PLS = game:GetService("Players")

local Debounces = { }

local Part = script.Parent
local moneySFX = Part.Money
local moneyEmitter = Part.ParticleEmitter
Part.Touched:Connect(function(hit)
    local Character = hit.Parent

	if Character:FindFirstChild("Humanoid") ~= nil then
        local Client = PLS:GetPlayerFromCharacter(Character)

        if Client then
            if Debounces[Client.UserId] then return end
			Debounces[Client.UserId] = true
			moneySFX:Play()
			moneyEmitter.Enabled = true
			Client.leaderstats.Money.Value += 25
			moneyEmitter.Enabled = false
		end
	end
end)
4 Likes