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

Can you help me with my Perk Shop?

Asked by 9 years ago

So I am having some problems with my Developer Product Perk Shop in my game. I have this script to allow people to get Speed, Health, and Gear when they buy one of my Developer products.

local MarketplaceService = game:GetService("MarketplaceService")
local AddHealthID = 22137556 --ENTER 
local AddSpeedID = 22137544  --PASS
local AddGearID = 22137570   --IDS
local AddGear2ID = 22137564   --IDS
local AddGear3ID = 22137575  --HERE
local AddGear4ID = 22137566   --IDS
local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
MarketplaceService.ProcessReceipt = function(receiptInfo) 
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    if PurchaseHistory:GetAsync(playerProductKey) then
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
    for i, player in ipairs(game.Players:GetPlayers()) do
        if player.userId == receiptInfo.PlayerId then
            if receiptInfo.ProductId == AddHealthID then
                player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + 50 --Adds 50 to their current health
                player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
            end
            if receiptInfo.ProductId == AddSpeedID then
                player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed + 15 -- Adds 5 to their current Walkspeed
            end
            if receiptInfo.ProductId == AddGearID then
                game.ReplicatedStorage.GravityCoil:Clone().Parent = player.Backpack -- put the gear in ReplicatedStorage then put the name of the gear in the GEARNAMEHERE
                -- game.ReplicatedStorage.GEARNAMEHERE:Clone().Parent = player.StarterGear -- Add this back in if you want them to get this item till they leave the game, Like renting it
            end
            if receiptInfo.ProductId == AddGear2ID then
                game.ReplicatedStorage.LockonLauncher:Clone().Parent = player.Backpack -- put the gear in ReplicatedStorage then put the name of the gear in the GEARNAMEHERE
                -- game.ReplicatedStorage.GEARNAMEHERE:Clone().Parent = player.StarterGear -- Add this back in if you want them to get this item till they leave the game, Like renting it
            end
            if receiptInfo.ProductId == AddGear3ID then
                game.ReplicatedStorage.JetPack:Clone().Parent = player.Backpack -- put the gear in ReplicatedStorage then put the name of the gear in the GEARNAMEHERE
                -- game.ReplicatedStorage.GEARNAMEHERE:Clone().Parent = player.StarterGear -- Add this back in if you want them to get this item till they leave the game, Like renting it
            end
            if receiptInfo.ProductId == AddGear4ID then
                game.ReplicatedStorage.EpicLaser:Clone().Parent = player.Backpack -- put the gear in ReplicatedStorage then put the name of the gear in the GEARNAMEHERE
                -- game.ReplicatedStorage.GEARNAMEHERE:Clone().Parent = player.StarterGear -- Add this back in if you
            end
        end
    end
    PurchaseHistory:SetAsync(playerProductKey, true)    
    return Enum.ProductPurchaseDecision.PurchaseGranted 
end

This works perfectly fine, but I added this script that also works, but it adds cash to there leader stats to use on things in the tycoon.

local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")

CASHID = 23781403

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("Cash")
                    if cs ~= nil then
                        cs.Value = cs.Value + 10,000 
                    end
                end
            end
        end
    end
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

These two scripts both work, but when they are both in the same game running at the same time the cash script causes the other script to completely stop working at all. It probably runs into something inside of the scripts that make them not compatible, but I was wondering how I could fix this problem. Maybe forming the two scripts together so they run as one? Please help me!

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your Problem

Stop using free models, they won't help you learn or enhance your skill at all.

Otherwise..

In your second script, when you define the ProcessReceipt of MarketplaceService, then it overwrites the preciously set ProcessReceipt. The ProcessReceipt property isn't like an event, it's a property that can be set to a function.. and only one function.. if you define two ProcessReceipts then it will be set as the latter.. it's not like an event, where if you make two of them then they will both occur.


How To Fix

Combine both of your ProcessReceipts!


Code

local rep = game.ReplicatedStorage
local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local MarketplaceService = game:GetService("MarketplaceService")
--Looks cleaner this way, huh?
local passes = {
    AddHealthID = 22137556 ,
    AddSpeedID = 22137544,
    CashID = 23781403,
    AddGear = {
        22137570,
        22137564,
        22137575,
        22137566
    }
}

MarketplaceService.ProcessReceipt = function(receiptInfo) 
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    if PurchaseHistory:GetAsync(playerProductKey) then
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
    for i, player in ipairs(game.Players:GetPlayers()) do
        if player.userId == receiptInfo.PlayerId then
            local hum = player.Character:WaitForChild('Humanoid')
            local bp = player.Backpack
            local sg = player.StarterGear
            --Health
            if receiptInfo.ProductId == passes.AddHealthID then
                hum.MaxHealth = hum.MaxHealth + 50
                hum.Health = hum.MaxHealth
            --Speed
            elseif receiptInfo.ProductId == passes.AddSpeedID then
                hum.WalkSpeed = hum.WalkSpeed + 15
            --Gear 1
            elseif receiptInfo.ProductId == passes.AddGear[1] then
                local tool = rep.GravityCoil
                tool:Clone().Parent = bp
                tool:Clone().Parent = sg
            --Gear 2
            elseif receiptInfo.ProductId == passes.AddGear[2] then
                local tool = rep.LockonLauncher
                tool:Clone().Parent = bp
                tool:Clone().Parent = sg
            --Gear 3
            elseif receiptInfo.ProductId == passes.AddGear[3] then
                local tool = rep.JetPack
                tool:Clone().Parent = bp
                tool:Clone().Parent = sg
            --Gear 4
            elseif receiptInfo.ProductId == passes.AddGear[4] then
                local tool = rep.EpicLaser
                tool:Clone().Parent = bp
                tool:Clone().Parent = sg
            --Money
            elseif receiptInfo.ProductId == passes.CashID then
                local stat = player.leaderstats.Cash
                stat.Value = stat.Value + 10,000
            end
        end
    end
    PurchaseHistory:SetAsync(playerProductKey, true)    
    return Enum.ProductPurchaseDecision.PurchaseGranted 
end
Ad

Answer this question