What to do if functions overwrite each other?

Local:

wait(0.4)

local tween = game:GetService("TweenService")
local mps = game:GetService("MarketplaceService")
local main = script.Parent.Parent.donateWins.main
local openButton = script.Parent.Parent.UnderGui.Wins.BuyWins

local player = game:GetService("Players").LocalPlayer
local ld = player:WaitForChild("leaderstats")

local DonateSound = game:GetService("SoundService").donate
--local donateID = 1600443774
--local donateID2 = 1600466409
--local donateID3 = 1600697377
--local donateID4 = 1600701485

local function openTrainMenu() -- ΠžΠ’ΠšΠ Π«Π’ΠΠ•Πœ
	main.Parent.close.Visible = true
	game:GetService("SoundService").click:Play()
	local Info = TweenInfo.new(0.15)
	local Tween = tween:Create(main, Info, {Position = UDim2.new(0.143, 0, 0.106, 0)})
	Tween:Play()

	game:GetService("Lighting").Blur.Enabled = true
	tween:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 90}):Play()
end

local function closeTrainMenu() -- Π—ΠΠšΠ Π«Π’ΠΠ•Πœ
	main.Parent.close.Visible = false
	game:GetService("SoundService").click:Play()
	local Info = TweenInfo.new(0.15)
	local Tween = tween:Create(main, Info, {Position = UDim2.new(0.143, 0, 1, 0)})
	Tween:Play()

	game:GetService("Lighting").Blur.Enabled = false
	tween:Create(game.Workspace.CurrentCamera, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70}):Play()
end

for i, button in main:GetChildren() do
	if button:FindFirstChild("Id") then -- add int value inside your button called "Id" and set the value of it to your id
		button.MouseButton1Click:Connect(function()
			mps:PromptProductPurchase(player, button.Id.Value)
		end)
		button.TouchTap:Connect(function()
			mps:PromptProductPurchase(player, button.Id.Value)
		end)
	end
end

openButton.MouseButton1Click:Connect(openTrainMenu)
openButton.TouchTap:Connect(openTrainMenu)

main.Parent.close.MouseButton1Click:Connect(closeTrainMenu)
main.Parent.close.TouchTap:Connect(closeTrainMenu)

Server:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local ids = {
	{Id = 1600443774,
		Amount = 140,},
	{Id = 1600466409,
		Amount = 350,},
	{Id = 1600697377,
		Amount = 910,},
	{Id = 1600701485,
		Amount = 3150,},
	{Id = 1603978937,
		Amount = 6300,},
	{Id = 1603979852,
		Amount = 12600,},
	{Id = 1603981728,
		Amount = 21000,}
}

local function AddMoney(receiptInfo)
	local ProductId = receiptInfo.ProductId
	local item
	for i, product in pairs(ids) do
		if product.Id == ProductId then
			item = product
			break
		end
	end
	if not item then return Enum.ProductPurchaseDecision.NotProcessedYet end
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then return Enum.ProductPurchaseDecision.NotProcessedYet end
	local leds = player:WaitForChild("leaderstats")
	local amountToGive
	for i, v in pairs(ids) do
		if v.Id == ProductId then
			amountToGive = v.Amount
			break
		end
	end
	if amountToGive then
		leds.Wins.Value += amountToGive
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

MarketplaceService.ProcessReceipt = AddMoney

So, here I have a donation for victories, the problem is that when you click and buy a donation the following error appears:


BUT, when I looked more closely at the error I saw that for some reason the game had taken a non-server script for victories and a server script for rebirths, here:


Rebirth Server:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

-- Data store for tracking purchases that were successfully processed
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")

local productFunctions = {} 
productFunctions[1604253727] = function(_receipt, player)
	if player and player:FindFirstChild("leaderstats") then
		player.leaderstats.Rebirth.Value = player.leaderstats.Rebirth.Value + 1
		game:GetService("SoundService").donate:Play()
		return true
	end
end

local function processReceipt(receiptInfo)
	-- Determine if the product was already granted by checking the data store
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
	local purchased = false
	local success, result, errorMessage

	success, errorMessage = pcall(function()
		purchased = purchaseHistoryStore:GetAsync(playerProductKey)
	end)
	-- If purchase was recorded, the product was already granted
	if success and purchased then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif not success then
		error("Data store error:" .. errorMessage)
	end

	-- Determine if the product was already granted by checking the data store  
	local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId

	local success, isPurchaseRecorded = pcall(function()
		return purchaseHistoryStore:UpdateAsync(playerProductKey, function(alreadyPurchased)
			if alreadyPurchased then
				return true
			end

			-- Find the player who made the purchase in the server
			local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
			if not player then
				-- The player probably left the game
				-- If they come back, the callback will be called again
				return nil
			end

			local handler = productFunctions[receiptInfo.ProductId]

			local success, result = pcall(handler, receiptInfo, player)
			-- If granting the product failed, do NOT record the purchase in datastores.
			if not success or not result then
				error("Failed to process a product purchase for ProductId: " .. tostring(receiptInfo.ProductId) .. " Player: " .. tostring(player) .. " Error: " .. tostring(result))
				return nil
			end

			-- Record the transcation in purchaseHistoryStore.
			return true
		end)
	end)

	if not success then
		error("Failed to process receipt due to data store error.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	elseif isPurchaseRecorded == nil then
		-- Didn't update the value in data store.
		return Enum.ProductPurchaseDecision.NotProcessedYet
	else	
		-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt

So the main question is why is the game messing up scripts? Even though it never did before.

In the end, as I was told scripts overwrite, but how do I do it so that scripts do not overwrite each other? I need to have two donations, for victories (script above) and for rebirth (script below). If you need it, here is my local script for rebirth

local main = script.Parent.Parent.RebirthGui.Main
local player = game:GetService("Players").LocalPlayer

local function donateRebirth()
	game:GetService("MarketplaceService"):PromptProductPurchase(player, 1604253727)

	game:GetService("SoundService").click:Play()
end

main.DonateRebirth.MouseButton1Click:Connect(donateRebirth)
main.DonateRebirth.TouchTap:Connect(donateRebirth)
1 Like
nitpicky thing

.MouseButton1Click() registers for mobile users too, so .TouchTap() isn’t needed