Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why wont these dev products work?

I am trying to make a GUI that sells dev products. In this case, i am trying to make it give you a certain amount of Credits (250) after purchasing a dev product. I can't seem to find the problem here... Can anyone help?

-- setup local variables
local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 20768662

-- define function that will be called when purchase finished
MarketplaceService.ProcessReceipt = function(receiptInfo) 

	-- find the player based on the PlayerId in receiptInfo
	for i, player in ipairs(game.Players:GetChildren()) do
		if player.userId == receiptInfo.PlayerId then
									
			-- check which product was purchased
			if receiptInfo.ProductId == productId then
				--handle purchase. In this case we are highering the player's Credits value			
    				plr.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + 250
				-- more feedback for the player.
				game.Workspace.DisplayScreen.SurfaceGui.TextBox.Text = player.Name .. " has purchased more credits."
					end
				end
			end
		end

2 comments

0

What's the error message?

0

The error message is: attempt to index global 'plr' (a nil value)

3 answers

You didn't define player.

Whenever you see that kind of error, remember that it means you have to define it. It doesn't know what plr is

Here put this code in a script then put the script in StarterPack:

local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
 
CASHID = 00000000 -- Put the Developer Product ID here and erase the 0's

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_product_" .. receiptInfo.ProductId
	local numberBought = ds:IncrementAsync(playerProductKey, 1)
	for i,v in pairs (game.Players:GetChildren()) do
		if v.userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == CASHID then
			
			
				lds = v:FindFirstChild("leaderstats")
				if lds ~= nil then
					cs = lds:FindFirstChild("Credits") --Change to your leaderstat
					if cs ~= nil then
						cs.Value = cs.Value + 250 --Change to the amount you want this to add
					end
				end
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted		
end

Don't forget to say this answered you and give it a thumbs up because I am working for a good reputation!

On line 16 change plr to player. Plr is nil to the script since it was never defined.

And add to line 19 or else the player will get credits on entry. return Enum.ProductPurchaseDecision.PurchaseGranted

Please refrain from reposting this question.

Any errors, just comment on this answer.


-- setup local variables
local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 20768662

-- define function that will be called when purchase finished
MarketplaceService.ProcessReceipt = function(receiptInfo) 

    -- find the player based on the PlayerId in receiptInfo
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then

            -- check which product was purchased
            if receiptInfo.ProductId == productId then
                --handle purchase. In this case we are highering the player's Credits value         
                    player.leaderstats.Credits.Value = plr.leaderstats.Credits.Value + 250
                -- more feedback for the player.
                game.Workspace.DisplayScreen.SurfaceGui.TextBox.Text = player.Name .. " has purchased more credits."
return Enum.ProductPurchaseDecision.PurchaseGranted --this is line 19.
                    end
                end
            end
        end

5 comments

0

Im sorry, but i am not that advanced at scripting... Can you please show me an example of what u meant to put at line 19?

0

This still doesnt work... can you please rephrase?

0

Still doesnt work...........

0

Oh, and it still says the same error

0

Are you using the script in my answer? It shouldn't be giving you a error. If it does, then post the line it is on (should be in blue text).

This discussion is preserved as it was. New answers, comments, and votes are closed.