I’ve been trying to make a gifting system for currency but sometimes It either gives the player twice as much or gives both the buyer and one you’re gifting to money. I’ve tried re-writing it but I can’t figure out why. I believe the table isn’t removing the proper one at the end of it.
local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Gifts = {}
local Rewards = require(script.Rewards)
MarketPlaceService.ProcessReceipt = function(receiptInfo)
print(receiptInfo)
local player
for i, Rewards in ipairs(Rewards.Rewards) do
if receiptInfo.ProductId == Rewards[1] then
if Rewards[2] == false then
player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
player.leaderstats.Coins.Value += Rewards[3]
elseif Rewards[2] == true then
for j, Gifts in pairs(Gifts) do
player = Players:GetPlayerByUserId(Gifts.to)
player.leaderstats.Coins.Value += Rewards[3]
print("Found The Gifter!")
table.remove(Gifts, j)
end
end
end
end
warn("Good Purchase")
return Enum.ProductPurchaseDecision.PurchaseGranted
end
ReplicatedStorage.Gift.OnServerEvent:Connect(function(Purchaser, ProductId, WhoTo)
if Purchaser.UserId ~= WhoTo.UserId then
local PurchasePrompt = MarketPlaceService:PromptProductPurchase(Purchaser, ProductId)
for _, gift in pairs(Gifts) do
if gift.from == Purchaser.UserId and gift.to == WhoTo.UserId then
return
end
end
table.insert(Gifts, {
from = Purchaser.UserId,
to = WhoTo.UserId
})
end
end)
That is my code, probably chaotic but I’ve added and removed so many things at this rate
1 Like
Sorry guys, forgot an if statement. It legit just clicked to me but I swear I tried it many times before
for j, Gifts in pairs(Gifts) do
if Gifts.from == receiptInfo.PlayerId then
player = Players:GetPlayerByUserId(Gifts.to)
player.leaderstats.Coins.Value += Rewards[3]
print("Found The Gifter!")
table.remove(Gifts, j)
end
end
I’ll leave this up as If anyone can find any problems with my script I would appreciate it, not somewhere I want to mess up this!
you should print-check the lengths of your tables to see if there are any duplicates. if there are duplicates with the same values and keys, then you can try to implement a duplicate-check to prevent duplicate insertions.
if the problem derives from other places, such as the event being received twice, then your handling with events could benefit from a sanity check, such as a debounce or verifying if the purchase is the same. there’s plenty of ways to do this.
3 Likes
I managed to fix the table stuff not being deleted (I believe) But I will definitely look into the debounce, verifying if the purchase is the same and checking if there is two of the same tables trying to be added.
Hello fellow doge this code has not been tested but would this work?
local MarketPlaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Gifts = {}
local RewardData = require(script.Rewards)
MarketPlaceService.ProcessReceipt = function(receiptInfo)
print(receiptInfo)
local player
for _, reward in ipairs(RewardData.Rewards) do
if receiptInfo.ProductId == reward[1] then
if not reward[2] then
player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if player and player:FindFirstChild("leaderstats") then
player.leaderstats.Coins.Value += reward[3]
end
else
for i, gift in pairs(Gifts) do
if gift.from == receiptInfo.PlayerId then
player = Players:GetPlayerByUserId(gift.to)
if player and player:FindFirstChild("leaderstats") then
player.leaderstats.Coins.Value += reward[3]
print("Found The Gifter!")
end
table.remove(Gifts, i)
break
end
end
end
break
end
end
warn("Good Purchase")
return Enum.ProductPurchaseDecision.PurchaseGranted
end
ReplicatedStorage.Gift.OnServerEvent:Connect(function(purchaser, productId, whoTo)
if purchaser.UserId ~= whoTo.UserId then
for _, gift in pairs(Gifts) do
if gift.from == purchaser.UserId and gift.to == whoTo.UserId then
return
end
end
table.insert(Gifts, {
from = purchaser.UserId,
to = whoTo.UserId
})
MarketPlaceService:PromptProductPurchase(purchaser, productId)
end
end)
Aha hey,
I’ll have a look and maybe pick out a few bits I believe will make it more secure but tbh anything other than my code is most likely more secure and better lol 