Error with coin shop

So the players of my game have an attribute called Coins that is setted up with this script.


local DataStoreService = game:GetService("DataStoreService")
local playerCoinsDataStore = DataStoreService:GetDataStore("PlayerCoins")

local function loadPlayerCoins(player)
	local playerUserId = "Player_" .. player.UserId
	local coins

	local success, err = pcall(function()
		coins = playerCoinsDataStore:GetAsync(playerUserId)
	end)

	if success then
		if coins then
			player:SetAttribute("Coins", coins)
		else
			player:SetAttribute("Coins", 0)
		end
	else
		warn("Error al cargar las monedas del jugador: " .. err)
	end
end

local function savePlayerCoins(player)
	local playerUserId = "Player_" .. player.UserId
	local coins = player:GetAttribute("Coins")

	local success, err = pcall(function()
		playerCoinsDataStore:SetAsync(playerUserId, coins)
	end)

	if not success then
		warn("Error al guardar las monedas del jugador: " .. err)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	loadPlayerCoins(player)

	player.AncestryChanged:Connect(function()
		if not player:IsDescendantOf(game) then
			savePlayerCoins(player)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	savePlayerCoins(player)
end)

And I have a script that should give the player coins, this is the script:

function processReceipt(info)
	local plr = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)
	local coins = plr:FindFirstChild("Coins")
	
	if plr then
		if info.ProductId == 1854435467 then
			--10
			coins.Value = coins.Value + 10
			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt

But I get this error, someone knows wheres the error?
image

you set coins as an attribute, you cant use findFirstChild() to get it considering it doesnt create a child.
Use GetAttibute() instead

local DataStoreService = game:GetService("DataStoreService")
local playerCoinsDataStore = DataStoreService:GetDataStore("PlayerCoins")

local function loadPlayerCoins(player)
    local playerUserId = "Player_" .. tostring(player.UserId)
    local coins

    local success, err = pcall(function()
        coins = playerCoinsDataStore:GetAsync(playerUserId)
    end)

    if success then
        player:SetAttribute("Coins", coins or 0)
    else
        warn("Error al cargar las monedas del jugador: " .. err)
    end
end

local function savePlayerCoins(player)
    local playerUserId = "Player_" .. tostring(player.UserId)
    local coins = player:GetAttribute("Coins") or 0

    local success, err = pcall(function()
        playerCoinsDataStore:SetAsync(playerUserId, coins)
    end)

    if not success then
        warn("Error al guardar las monedas del jugador: " .. err)
    end
end

game.Players.PlayerAdded:Connect(function(player)
    loadPlayerCoins(player)

    player.AncestryChanged:Connect(function()
        if not player:IsDescendantOf(game) then
            savePlayerCoins(player)
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    savePlayerCoins(player)
end)

The changes made:

  1. local playerUserId = "Player_" .. player.UserId has been corrected to local playerUserId = "Player_" .. tostring(player.UserId).
  2. The condition player:SetAttribute("Coins", coins) has been corrected to player:SetAttribute("Coins", coins or 0).
  3. The condition player:GetAttribute("Coins") has been corrected to player:GetAttribute("Coins", 0).

For the processReceipt function, here is an example of how it might look after fixing the mentioned issues:

local function processReceipt(info)
    local player = game.Players:GetPlayerByUserId(info.PlayerId)
    
    if player then
        if info.ProductId == 1854435467 then
            -- Process the receipt
        end
    end
end

The changes made:

  1. The player parameter is changed to plr.
  2. The condition if plr then is checking if the player object is not nil.
  3. The line return without any arguments is removed.

Be sure to integrate and test both scripts k

1 Like

TYYY! Now it works perfectly!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.