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

Why wont this award sword?

Asked by 9 years ago
--LocalScript inside button
local buyButton = script.Parent
local productId = 20518668

buyButton.MouseButton1Click:connect(function()
    game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productId)
end)

local MarketplaceService = Game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 20518668
MarketplaceService.ProcessReceipt = function(receiptInfo) 
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
            if receiptInfo.ProductId == productId then
                local Item = game.ReplicatedStorage.Swords.Sword
                Item:Clone().Parent = player.Backpack
                Item:Clone().Parent = player.StarterGear
            end
        end
    end 
    local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
    ds:IncrementAsync(playerProductKey, 1)  
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

I want this set so once a player buys a dev product they get a sword in their inv. Please help.

2 answers

Log in to vote
1
Answered by 9 years ago

I don't think that you can get services from a local script, so you have to use both a normal script and local script.

Here is what the scripts might look like:

--Normal script in ServerScriptStorage
local MarketplaceService = Game:GetService("MarketplaceService")
local Func = Instance.new("RemoteFunction" , game.ReplicatedStorage)
Func.Name = "PromptPlayer"
Func.OnServerInvoke = function(PlayerToPrompt , productID)
    MarketplaceService:PromptProductPurchase(PlayerToPrompt , productID)
end

Here is the local script:

local buyButton = script.Parent
local ProductID = 20518668
buyButton.MouseButton1Click:connect(function()
    game.ReplicatedStorage.PromptPlayer:InvokeServer(ProductID)
end)

Now you have to process the "receipt" to award the sword. Create a separate script and put it in ServerScriptStorage.

local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local sword = 20518668
MarketplaceService.ProcessReceipt = function(receiptInfo) 
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    for i, player in ipairs(game.Players:GetChildren()) do
        if player.userId == receiptInfo.PlayerId then
            if receiptInfo.ProductId == sword then
                local Item = game.ReplicatedStorage.Swords.Sword
                Item:Clone().Parent = player.Backpack
                Item:Clone().Parent = player.StarterGear
        player:SaveBoolean("HasSword" , true)
            end
        end
    end 

    ds:SetAsync(playerProductKey , true)
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

If you want the player to always have the sword, add this normal script to ServerScriptStorage

local Players = game.Players

function onPlayerAdded(Player)
    if Player:WaitForDataReady() then
        if Player:LoadBoolean("HasSword") then
            local Item = game.ReplicatedStorage.Swords.Sword
                    Item:Clone().Parent = Player.Backpack
                    Item:Clone().Parent = Player.StarterGear
        end
    end
end

Players.PlayerAdded:connect(onPlayerAdded)

If this doesn't work, please tell me.

0
I want to know if this works. If it didn't, I will edit this, otherwise you can let me know if it did. xxracerdudexx 75 — 9y
0
localscript in button? NinjoOnline 1146 — 9y
0
Yes xxracerdudexx 75 — 9y
0
Ok it sorta works, it gives sword, but when the player leaves and rejoins they dont have the sword NinjoOnline 1146 — 9y
View all comments (18 more)
0
either I want their swords with them forever and then they cant purchase pass again, or else, i can leave it there and just make them buy the pass whenever they want a sword NinjoOnline 1146 — 9y
0
Ok I will try to do that xxracerdudexx 75 — 9y
0
ok cool, thanks, its just i would prefer people to buy the pass once and they keep the item forever instead of when they leave they have to buy the poduct again. If you cant then I can live with it for now, but if you can try then thank you :) NinjoOnline 1146 — 9y
0
I edited in a new block of code. It will give the player the sword if they bought the DP. Tell me if it works or not,k? xxracerdudexx 75 — 9y
0
so everything is the exact same but the last which is new code? NinjoOnline 1146 — 9y
0
Yes xxracerdudexx 75 — 9y
0
dosent work, i think it has something to with new script. It gives sword on purchase, but dosent save NinjoOnline 1146 — 9y
0
Ok Ill try to fix that xxracerdudexx 75 — 9y
0
I think I fixed the mistake xxracerdudexx 75 — 9y
0
Works, had to change some stuff but it works :D thanks +1 and answer accepted :D NinjoOnline 1146 — 9y
0
Yay! 1 thing: can you tell me what you changed? xxracerdudexx 75 — 9y
0
ok, in the one that checks if the player has brought thing then give it back tot them, it says player.Backpack, but should be Player.Backpack NinjoOnline 1146 — 9y
0
Oh ok. The reason why it was lowercase "p" is because I copied it and did not check what has been pasted. I'm going to edit the answer so anyone else using the code won't have errors xxracerdudexx 75 — 9y
0
yeah, i didnt need to test, and I knew that yo had copy and pasted :P oh and does it need to have Local Players = game.Players, cause you never used Players in the code NinjoOnline 1146 — 9y
0
The reason why I added that is because I needed to use the PlayerAdded event, but I was so stupid I forgot to add the connection line, and when you told me it wasn't working I saw the mistake and fixed it. xxracerdudexx 75 — 9y
0
it dosent work when you have multiple swords ?? ony 1 works, the others dont give the swords NinjoOnline 1146 — 9y
0
You mean more than one sword? Ok I'll edit it so it gives more than one sword xxracerdudexx 75 — 9y
0
Actually I am not sure how to do it xxracerdudexx 75 — 9y
Ad
Log in to vote
-2
Answered by 9 years ago

You have a typo on line 13, It says "ipairs" and it should say pairs.

0
Both ipairs and pairs are valid iterate functions Perci1 4988 — 9y

Answer this question