Make script only give leaderstats value once

I have this script which gives the player 100 in-game money if they own the gamepass I made. The problem is, it gives them 100 cash each time they join. Is there a way to fix this?

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 19020734

local function onPlayerAdded(player)

	local hasPass = false

	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)


	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true then
		print(player.Name .. " owns the game pass with ID " .. gamePassID)

		player:WaitForChild("leaderstats"):WaitForChild("Money").Value +=100
	
	end
end


Players.PlayerAdded:Connect(onPlayerAdded)

Use devproducts, they’re made for repeatable purchases

https://developer.roblox.com/en-us/articles/Developer-Products-In-Game-Purchases

1 Like

I only want 100 cash to be given once if the user owns the gamepass.

Use FindFirstChild instead of WaitForChild.


local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 19020734

local function onPlayerAdded(player)

	local hasPass = false

	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)


	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true then
		print(player.Name .. " owns the game pass with ID " .. gamePassID)

		player:FindFirstChild("leaderstats"):FindFirstChild("Money").Value +=100
	
	end
end


Players.PlayerAdded:Connect(onPlayerAdded)

Possibly make a boolvalue, if it’s true you know they got the 100 cash so you can just do

if value.Value == true then return end

Did you read the article


Why would you want it to be a gamepass though? If it’s a gamepass then they can only get it once but if you use dev products like @Turned_Away has said, they can by it multiple times while it gives 100 per purchase

1 Like

So I can also check if you bought a dev product?

Like this?

local MarketPlaceService = game:GetService("MarketplaceService")

local Players = game:GetService("Players")

local productId = 'productid'

script.Parent.MouseButton1Down:Connect(function(promptPurchase)

local player = Players.LocalPlayer

MarketPlaceService:PromptProductPurchase(player, productId)

end)

Then fire a remote event to give the player the cash?

Yes you should be able to. Here is a video by “TheDevKing” on developer products.

use devproducts lol, why would u want it to be a gamepass

Yeah I kind of forgot about devproudcts :sweat_smile: