Problem: the player buys funds, but cannot use the purchased funds

I’m new to scripting, I’ve been learning for a month, I can’t figure it out myself, so I’m asking for help from you, more experienced people.

  1. ** What do you want to accomplish? ** I want someone to explain to me the problem why when a player buys funds, he can’t use them.

  2. ** What is the problem? ** I am creating a Tycoon game, and there is a problem in that when a player buys funds in my store with robuxes, they are credited to him in the leaderboard, but he can’t just use them, i.e. buy buttons etc. and also when a player collects funds from a collector, the funds he bought just disappear.

  3. **What solutions have you tried so far? ** Yes, I saw a similar article on the forum and borrowed the script from there and changed it a bit, but that article did not solve the problem, I also tried to solve the problem myself, but all to no avail.

I will attach here two codes that I have.

ServerStorageScript:Script
 local MarketplaceService = game:GetService("MarketplaceService") 
local DatastoreService = game:GetService("DataStoreService") 

local PreviousPurchases = DatastoreService:GetDataStore("PreviousPurchases")

local productFunctions = {} 

local cash5000 = 1823229791 -- 5000 cash id --
local cash15000 = 1823230874 -- 15000 cash id --
local cash50000 = 1823230999 -- 50000 cash id --

local function giveCash(plr, amt) 
	wait(1)
	plr.leaderstats.Cash.Value += amt
end

productFunctions[cash5000] = function(info, plr) -- gives plr 5000 cash
	giveCash(plr, 5000)
end

productFunctions[cash15000] = function(info, plr) -- gives plr 15000 cash
	giveCash(plr, 15000)
end

productFunctions[cash50000] = function(info, plr) -- gives plr 50000 cash
	giveCash(plr, 50000)
end


local function procRec(reciept)


	local ID = reciept.PlayerId.."-"..reciept.PurchaseId 

	if PreviousPurchases:GetAsync(ID) then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	local player = game.Players:GetPlayerByUserId(reciept.PlayerId)
	if not player then return Enum.ProductPurchaseDecision.NotProcessedYet end
	print(1)
	local handler = productFunctions[reciept.ProductId]
	print(2)
	local s, e = pcall(function()
		handler(reciept, player)
	end)

	if s then print("supposedly went through") return Enum.ProductPurchaseDesicion.PurchaseGranted elseif not s then warn(e) return Enum.ProductPurchaseDecision.NotProcessedYet end
end
MarketplaceService.ProcessReceipt = procRec

LocalScript:

local player = game.Players.LocalPlayer
local Market = game:GetService("MarketplaceService")
local ProductId = 1823229791

script.Parent.MouseButton1Click:Connect(function()
	Market:PromptProductPurchase(player, ProductId)
end)

can you format the code, i cant understand anything

formatted the code now it should be clear

can you help with this problem?

Try to use server script instead of LocalScript in second script

what should I put in place of localplayer?

is not this a gui?
then script.Parent.Parent.Parent.Parent should refer the player

Did you try using remote events?