I want to make it so that I can reuse this script for multiple purchasable objects. The developer product is like a force purchase for a tycoon object if they don’t have enough currency to buy it. I tried using a developer product, but it fires random purchases rather than the one I want it to, due to having the same product ID. How do I go about this?
Current script:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local thisButton = script.Parent
local function processPurchase(player, button)
player.leaderstats.Rupees.Value = math.max(player.leaderstats.Rupees.Value - button.price.Value, 0)
ReplicatedStorage:FindFirstChild(button.Parent.Name):FindFirstChild(button.modelName.Value).Parent = button.Parent
if button.Parent:FindFirstChild("tableDecorLamp")
and button.Parent:FindFirstChild("tableDecorLotion")
and button.Parent:FindFirstChild("Old Timey Bed") then
ReplicatedStorage.room1Complete:FireClient(player, button.Parent.Name)
end
button.Parent = ReplicatedStorage:FindFirstChild(button.Parent.Name)
ReplicatedStorage.purchasedEvent:FireClient(player)
end
thisButton.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then return end
if player.plotAssigned.Value == thisButton.Parent.Name then
if player.leaderstats.Rupees.Value >= thisButton.price.Value then
processPurchase(player, thisButton)
else
--I WANT IT TO PROMPT PURCHASE HERE--
end
end
end)
Yes, but I will be using the same developer product for multiple purchases within the game. The issue is, my previous script couldn’t tell which is being purchased, which caused the wrong thing to be purchased in-game.
Store the object that they were prompted to purchase in a variable/table, and when it’s processed, grant them that object.
Remember to remove it when they leave or the purchase is finished to prevent memory leaks.
When they are prompted to purchase the item, remember what they were prompted to purchase for.
You can do something like this:
-- Optimally you'd want to store pending purchases and what they were for in data stores to ensure they are granted the product.
-- This is just a basic example.
local pendingPurchases: {[Player]: Instance} = {}
-- Clean up
Players.PlayerRemoving:Connect(function(player: Player)
pendingPurchases[player] = nil
end)
local function promptPurchase(player: Player, button: Instance)
pendingPurchases[player] = button
end
-- ... And when processing a purchase receipt, read from pendingPurchases or relevant datastore to grant them their product.