Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why wont these dev products work?

Asked by 9 years ago

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
0
What's the error message? CardboardRocks 215 — 9y
0
The error message is: attempt to index global 'plr' (a nil value) omrisoso 10 — 9y

3 answers

Log in to vote
3
Answered by
Relatch 550 Moderation Voter
9 years ago

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

Ad
Log in to vote
3
Answered by 9 years ago

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!

Log in to vote
-1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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
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? omrisoso 10 — 9y
0
This still doesnt work... can you please rephrase? omrisoso 10 — 9y
0
Still doesnt work........... omrisoso 10 — 9y
0
Oh, and it still says the same error omrisoso 10 — 9y
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). M39a9am3R 3210 — 9y

Answer this question