Local Side:

local MPS = game:GetService("MarketplaceService")
local remote = script.Parent.Parent.StartCheckout
local receipt = game.ReplicatedStorage.Receipt
local status = script.Parent.Status
local assetId = nil
local player = game.Players.LocalPlayer

function requestispurchased(item)
	local IsPurchased = game.ReplicatedStorage.RequestIsPurchased:InvokeServer(item)
	return IsPurchased
end

remote.Event:Connect(function(item)
	local got = requestispurchased(item)
	local player = game.Players.LocalPlayer
		if got == item.Name then
			status.Text = "Already Purchased!"
			wait(3)
			script.Parent:TweenPosition(UDim2.new(1,0,0,0),"Out","Quad",0.5)
		else
			-- Tween
			script.Parent:TweenPosition(UDim2.new(0,0,0,0),"Out","Quad",0.5)
			local product = item.ID
			assetId = item.ID
			wait(1)
			status.Text = "Prompting Purchase"
			MPS:PromptProductPurchase(player,product)
		end
end)

receipt.OnClientEvent:Connect(function(isPurchased)
	if isPurchased == "Success" then
		status.Text = "Purchase Successful!"
		wait(3)
		script.Parent:TweenPosition(UDim2.new(1,0,0,0),"Out","Quad",0.5)
	elseif isPurchased == "Failed" then
		status.Text = "Purchase Failed!"
		wait(3)
		script.Parent:TweenPosition(UDim2.new(1,0,0,0),"Out","Quad",0.5)
	end
end)

Server Side:

local DataStore2 = require(game.ServerScriptService:WaitForChild("MainModule"))

local ProductCodes = {
	["DropSlide"] = 1,
	["RM"] = 2,
	["SMRTFIRE"] = 3,
}

game.Players.PlayerAdded:Connect(function(player)
	local productStore = DataStore2("products", player)

	productStore:BeforeInitialGet(function(products)
		--BeforeInitialGet IS NOT CALLED if there is no value in the data store.
		--That means that the value we have now is serialized.
		--The format of this value looks something like {1, 2, 3}.
		--We're going to change this to the string representations to make it much easier to code with.
		local deserialized = {}

		for _,itemId in pairs(products) do
			--You could optimize this code, but this is just to make the example easier.
			for key,value in pairs(ProductCodes) do
				if value == itemId then --The value in the serialized inventory matches the value in Items.
					table.insert(deserialized, key) --Add the string version to our deserialization.
				end
			end
		end
	return deserialized
end)
	productStore:BeforeSave(function(products)
		--BeforeSave is called with the DESERIALIZED inventory. Whatever this returns is what will be saved.
		--The "inventory" parameter looks something like {"Item1", "Item2", "Item3"}.
		--We're going to save this as {1, 2, 3} as a storage optimization.
		local serialized = {}

		for _,itemName in pairs(products) do
			table.insert(serialized, ProductCodes[itemName]) --"Item1" -> 1, etc
		end

		return serialized
	end)

	productStore:Get({})
end)

Server Side (WhenRequested):

local DataStore2 = require(game.ServerScriptService:WaitForChild("MainModule"))

function CheckIfPurchased(player,item)
	local productStore = DataStore2("products",player)
	local isPurchased = productStore:Get(item)
	return isPurchased
end


game.ReplicatedStorage.RequestIsPurchased.OnServerInvoke = CheckIfPurchased

The whole developer product thing just saves it to the data store. I know I used the tutorial's datastore 2 thing but it did clear a lot up for me, thats why.

I don't know where I could have messed up but the whole requesting thing isn't working, I have tried many ways to fix it but none of my methods worked. If anyone can help me I would really appreciate it.

2 comments

0

A better response for this would come from the dev behind DataStore2 (Ask him, he's open to it).

0

I'm the developer behind DataStore2 and my DMs are always open on Twitter (Kampfkarren, in fact respond to me there, I'm only here on a whim). Your issue is you don't seem to be calling :Set ever.