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

Why wont this award sword?

--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

✓ Accepted answer

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.

22 comments

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.

0

localscript in button?

0

Yes

0

Ok it sorta works, it gives sword, but when the player leaves and rejoins they dont have the sword

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

0

Ok I will try to do that

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 :)

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?

0

so everything is the exact same but the last which is new code?

0

Yes

0

dosent work, i think it has something to with new script. It gives sword on purchase, but dosent save

0

Ok Ill try to fix that

0

I think I fixed the mistake

0

Works, had to change some stuff but it works :D thanks +1 and answer accepted :D

0

Yay! 1 thing: can you tell me what you changed?

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

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

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

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.

0

it dosent work when you have multiple swords ?? ony 1 works, the others dont give the swords

0

You mean more than one sword? Ok I'll edit it so it gives more than one sword

0

Actually I am not sure how to do it

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

1 comment

0

Both ipairs and pairs are valid iterate functions

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