Developer Products Not Working Properly [SOLVED]

Heya! I’ve made a post about this but no one responded and I haven’t found anything to fix my problem and now a new problem occured so… when I buy 100 “Ocula”, it awards 204,000 Ocula instead. Here is the local script that prompts the player to buy the developer product:

local MarketPlaceService = game:GetService("MarketplaceService")
local id = 1635933025

script.Parent.MouseButton1Click:Connect(function()
	MarketPlaceService:PromptProductPurchase(game.Players.LocalPlayer, id)
end)

and here is the server script in ServerScriptService to check when the player buys the 100 Ocula

local ourPlayer = nil
game.Players.PlayerAdded:Connect(function(player)
	ourPlayer = player
end)

wait(2)
	
local tween = game:GetService("TweenService"):Create(ourPlayer:WaitForChild("PlayerGui"):WaitForChild("ShopMenu"):WaitForChild("ShopMenu"):WaitForChild("OculaUpdate"), TweenInfo.new(3), {BackgroundTransparency = 1})
local tween2 = game:GetService("TweenService"):Create(ourPlayer:WaitForChild("PlayerGui"):WaitForChild("ShopMenu"):WaitForChild("ShopMenu"):WaitForChild("OculaUpdate").TextLabel, TweenInfo.new(3), {TextTransparency = 1})

--100 Ocula
game.MarketplaceService.ProcessReceipt = function(reciptInfo)
	local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
	if reciptInfo.ProductId == 1635933025 then
		ourPlayer:WaitForChild("Ocula").Value = ourPlayer:WaitForChild("Ocula").Value + 100
		tween:Cancel()
		tween2:Cancel()
		ourPlayer.PlayerGui.ShopMenu.ShopMenu.OculaUpdate.BackgroundTransparency = 0
		ourPlayer.PlayerGui.ShopMenu.ShopMenu.OculaUpdate.TextLabel.TextTransparency = 0
		ourPlayer.PlayerGui.ShopMenu.ShopMenu.OculaUpdate.TextLabel.Text = "You've successfully bought 100 Ocula!"
		wait(1.5)
		tween:Play()
		tween2:Play()
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

Please help, as I do not know why it is awarding 204000 instead of 100. Thank you!

3 Likes

Can you put a print statement before you award the 100 to make sure what Value it has? Also unless this is a single player game your ourPlayer will always be the last person added to the game so they will get all the Ocula.

1 Like

So, it seems that I have 203900 whenever I join the game and even when I set it back to 0 in the explorer when I test the game, it goes back to 204000 whenever I buy 100. And thank you for bringing the “ourPlayer” thing to my attention, which… how else do I refer to the player?

1 Like

You answered this yourself in the script.
local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
This defines the player who purchased the item. Use plr instead of ourPlayer

Well I can’t get the player when I put this outside of the ProcessReceipt thing and I need the tweens to be referred to outside of the ProcessReceipt thing to cancel them whenever someone buys another one of these.

Storing those tweens outside the function is a bad coding practice. The function needs to be self-contained.

So you need to go to where you either create that NumberValue in code or where you added it to your player and see why it isn’t 0 to begin with.

As for the tweens @CodeJared is right about bad coding practice and things will be messy. Looking at what you are trying to do you should really have a RemoteEvent you are firing back to the client to handle the tween and if it gets a second event you can always stop the first and restart it.

Mmhmm… but then if someone buys another dev product while it’s fading out, I won’t be able to cancel it unless I refer to the tweens each time they buy a dev product, which I think would not cancel other tweens that are currently happening.

To add to this, the tweens should not be done on the server. Anything dealing with GUIs should be done locally.

So then how do I find out if the player buys it locally when this all happens server-side.

From line 15 to 24 in server script, change ourPlayer to plr.

The server needs to send a RemoteEvent that the client can pick up on.

1 Like

I spent an hour doing this only for 100,000 to work and nothing else. Here’s the new server script:

--100 Ocula
game.MarketplaceService.ProcessReceipt = function(reciptInfo)
	local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
	if reciptInfo.ProductId == 1635933025 then
		game.ReplicatedStorage.Remotes["O100"]:FireClient(plr)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

--500 Ocula
game.MarketplaceService.ProcessReceipt = function(reciptInfo)
	local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
	if reciptInfo.ProductId == 1635936556 then
		game.ReplicatedStorage.Remotes["O500"]:FireClient(plr)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

--1000 Ocula
game.MarketplaceService.ProcessReceipt = function(reciptInfo)
	local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
	if reciptInfo.ProductId == 1635936786 then
		game.ReplicatedStorage.Remotes["O1000"]:FireClient(plr)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

--10,000 Ocula
game.MarketplaceService.ProcessReceipt = function(reciptInfo)
	local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
	if reciptInfo.ProductId == 1635937101 then
		game.ReplicatedStorage.Remotes["O10000"]:FireClient(plr)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

--100,000 Ocula
game.MarketplaceService.ProcessReceipt = function(reciptInfo)
	local plr = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
	if reciptInfo.ProductId == 1635937696 then
		game.ReplicatedStorage.Remotes["O100000"]:FireClient(plr)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

Here’s the local script

local tween = game:GetService("TweenService"):Create(script.Parent.OculaUpdate, TweenInfo.new(3), {BackgroundTransparency = 1})
local tween2 = game:GetService("TweenService"):Create(script.Parent.OculaUpdate.TextLabel, TweenInfo.new(3), {TextTransparency = 1})

game.ReplicatedStorage.Remotes["O100"].OnClientEvent:Connect(function()
	game.Players.LocalPlayer:WaitForChild("Ocula").Value = game.Players.LocalPlayer:WaitForChild("Ocula").Value + 100
	tween:Cancel()
	tween2:Cancel()
	script.Parent.OculaUpdate.BackgroundTransparency = 0
	script.Parent.OculaUpdate.TextLabel.TextTransparency = 0
	script.Parent.OculaUpdate.TextLabel.Text = "You've successfully bought 100 Ocula!"
	wait(1.5)
	tween:Play()
	tween2:Play()
end)

game.ReplicatedStorage.Remotes["O500"].OnClientEvent:Connect(function()
	game.Players.LocalPlayer:WaitForChild("Ocula").Value = game.Players.LocalPlayer:WaitForChild("Ocula").Value + 500
	tween:Cancel()
	tween2:Cancel()
	script.Parent.OculaUpdate.BackgroundTransparency = 0
	script.Parent.OculaUpdate.TextLabel.TextTransparency = 0
	script.Parent.OculaUpdate.TextLabel.Text = "You've successfully bought 500 Ocula!"
	wait(1.5)
	tween:Play()
	tween2:Play()
end)

game.ReplicatedStorage.Remotes["O1000"].OnClientEvent:Connect(function()
	game.Players.LocalPlayer:WaitForChild("Ocula").Value = game.Players.LocalPlayer:WaitForChild("Ocula").Value + 1000
	tween:Cancel()
	tween2:Cancel()
	script.Parent.OculaUpdate.BackgroundTransparency = 0
	script.Parent.OculaUpdate.TextLabel.TextTransparency = 0
	script.Parent.OculaUpdate.TextLabel.Text = "You've successfully bought 1000 Ocula!"
	wait(1.5)
	tween:Play()
	tween2:Play()
end)

game.ReplicatedStorage.Remotes["O10000"].OnClientEvent:Connect(function()
	game.Players.LocalPlayer:WaitForChild("Ocula").Value = game.Players.LocalPlayer:WaitForChild("Ocula").Value + 10000
	tween:Cancel()
	tween2:Cancel()
	script.Parent.OculaUpdate.BackgroundTransparency = 0
	script.Parent.OculaUpdate.TextLabel.TextTransparency = 0
	script.Parent.OculaUpdate.TextLabel.Text = "You've successfully bought 10,000 Ocula!"
	wait(1.5)
	tween:Play()
	tween2:Play()
end)

game.ReplicatedStorage.Remotes["O100000"].OnClientEvent:Connect(function()
	game.Players.LocalPlayer:WaitForChild("Ocula").Value = game.Players.LocalPlayer:WaitForChild("Ocula").Value + 100000
	tween:Cancel()
	tween2:Cancel()
	script.Parent.OculaUpdate.BackgroundTransparency = 0
	script.Parent.OculaUpdate.TextLabel.TextTransparency = 0
	script.Parent.OculaUpdate.TextLabel.Text = "You've successfully bought 100,000 Ocula!"
	wait(1.5)
	tween:Play()
	tween2:Play()
end)

script.Parent.Ocula.OculusA.MouseButton1Click:Connect(function()
	game.MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, 1635933025)
end)

script.Parent.Ocula.OculusB.MouseButton1Click:Connect(function()
	game.MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, 1635936556)
end)

script.Parent.Ocula.OculusC.MouseButton1Click:Connect(function()
	game.MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, 1635936786)
end)

script.Parent.Ocula.OculusD.MouseButton1Click:Connect(function()
	game.MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, 1635937101)
end)

script.Parent.Ocula.OculusE.MouseButton1Click:Connect(function()
	game.MarketplaceService:PromptProductPurchase(game.Players.LocalPlayer, 1635937696)
end)

and, for the funsies, here’s the script that saves and loads the Ocula if you can find out why it starts at 203900

-- Define your data store name
local DATA_STORE_NAME = "OculusDataStore"

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore(DATA_STORE_NAME)

local function savePyroPoints(player)
	local pyroPointsValue = player:FindFirstChild("Ocula")
	if pyroPointsValue then
		local success, error = pcall(function()
			PlayerDataStore:SetAsync(tostring(player.UserId), pyroPointsValue.Value)
		end)
		if not success then
			warn("Error saving Ocula for player " .. player.Name .. ": " .. error)
		end
	end
end

local function loadPyroPoints(player)
	local success, value = pcall(function()
		return PlayerDataStore:GetAsync(tostring(player.UserId))
	end)

	if success then
		local pyroPointsValue = Instance.new("IntValue")
		pyroPointsValue.Name = "Ocula"
		pyroPointsValue.Value = value
		pyroPointsValue.Parent = player
	else
		warn("Error loading Ocula for player " .. player.Name)
	end
end

game.Players.PlayerRemoving:Connect(savePyroPoints)

game.Players.PlayerAdded:Connect(function(player)
	loadPyroPoints(player)
end)

You are adding the Oculas after purchase in a local script, move it to the server script.
Try using RemoveAsync(userId) on the Oculus DataStore to delete your data and see if it’s just your data for the extra amount of them

It’s the same result whether it’s on the server or client. And, for some reason buying the only one you can (100,000 Ocula) first sets it to 304,900 and then keeps adding 100,000 each purchase.

“gleuf” on Discord solved this in like 10 minutes, here’s the code sample he gave me:

local function onPromptProductPurchaseFinished(userId, purchasedProductID, purchaseSuccess)
    local player = Players:GetPlayerByUserId(userId)

    if purchaseSuccess and purchasedProductID == ProductID then
        -- Do something
    end
end

MarketplaceService.PromptProductPurchaseFinished:Connect(onPromptProductPurchaseFinished)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.