Too many purchases in one single script?

I made a server script for my game with the purpose of managing all product purchases within the game, however I have noticed that after making certain purchases (involving long "wait()"s) the script just kinda conks out.

📜 Here's a little exemplary snippet from the script (probably causing the error)
	elseif receiptInfo.ProductId == 1660053718 then
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				p.PlayerGui.Default.Blinded.Visible = true
			end
		end

		local TextLabel = Instance.new("TextLabel")
		TextLabel.Text = player.Name .. " has blinded everyone for 60s!"
		TextLabel.Font = Enum.Font.FredokaOne
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)
		TextLabel.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Default
		TextLabel.Visible = true
		
		Granted = true
		
		wait(55)

		TextLabel:Destroy()
		for _, p in pairs(game.Players:GetPlayers()) do
			p.PlayerGui.Default.Blinded.Visible = false
		end

	elseif receiptInfo.ProductId == 1660090129 then
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				if p.Character and p.Character:FindFirstChild("HumanoidRootPart") then
					local Humanoid = p.Character.Humanoid
					Humanoid.WalkSpeed = Humanoid.WalkSpeed / 2
				end
			end
		end
		local TextLabel = Instance.new("TextLabel")
		TextLabel.Parent = game:GetService("Players"):WaitForChild("PlayerGui"):WaitForChild("Default")
		TextLabel.Text = player.Name .. " has slowed everyone down for 60s!"
		TextLabel.FontFace = Font.fromName("FredokaOne")
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)

		TextLabel.Visible = true
		
		Granted = true
		
		wait(5)

		TextLabel.Visible = false
		TextLabel:Destroy()
		
		wait (55)
		
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				if p.Character and p.Character:FindFirstChild("HumanoidRootPart") then
					local Humanoid = p.Character.Humanoid

					Humanoid.WalkSpeed = 16
				end
			end
		end

Should I, instead of having all purchases stored in one single script, allocate all of the purchases into their own server script? What other solutions are there to solve this?

📜 Here's the server script in full for more information:
-- INDEX:

-- PRODUCTS AND TROLLS
-- 1. SKIP STAGE
-- 2. SKIP STAGE ALL
-- 3. UNDO STAGE ALL
-- 4. NUKE SERVER
-- 5. BLIND ALL
-- 6. SLOW ALL
-- 7. SPEED ALL
-- 8. LOW GRAVITY

local productFunctions = {}


local function addDonation(plr, amount)
	local Donate = plr.DonatedFolder.Donation
	Donate.Value = Donate.Value + amount
end


---------------------------------------------------------------------------------------------------

local marketPlaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local GUI = game:GetService("StarterGui")

marketPlaceService.ProcessReceipt = function(receiptInfo)
	
	local Granted = false

	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	
	if receiptInfo.ProductId == 1658627474 then
		player.leaderstats.Stage.Value += 1
		wait(1)
		player:LoadCharacter()
		
		Granted = true
	elseif receiptInfo.ProductId == 1659261655 then
		local playername = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		for _, p in pairs(players:GetPlayers()) do
			p.leaderstats.Stage.Value += 1
			wait(1)
			p:LoadCharacter()

			local TextLabel = Instance.new("TextLabel")
			TextLabel.Parent = p.PlayerGui.Default
			TextLabel.Text = p.Name .. " has skipped everyone's stage!"
			TextLabel.FontFace = Font.fromName("FredokaOne")
			TextLabel.TextScaled = true
			TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
			TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
			TextLabel.TextColor3 = Color3.new(158, 0, 0)

			TextLabel.Visible = true

			wait(5)

			TextLabel.Visible = false
		end
		Granted = true
	elseif receiptInfo.ProductId == 1660050340 then
		local playername = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		for _, p in pairs(players:GetPlayers()) do
			if p.Name ~= playername then
				if p.Character.HumanoidRootPart then
					p.leaderstats.Stage.Value -= 1
					wait(1)
					p:LoadCharacter()
				end
			end
		end
		local TextLabel = Instance.new("TextLabel")
		TextLabel.Parent = player.PlayerGui.Default
		TextLabel.Text = player.Name .. " has undone everyone's stages!"
		TextLabel.FontFace = Font.fromName("FredokaOne")
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)
		TextLabel.Visible = true
		
		Granted = true
		
		wait(5)

		TextLabel.Visible = false

	elseif receiptInfo.ProductId == 1658851386 then
		for _, p in pairs(players:GetPlayers()) do
			if p ~= player then
				if p.Character.HumanoidRootPart then
					local explosion = Instance.new("Explosion")
					explosion.Position = CFrame.new(p.HumanoidRootPart.Position)
				end
			end
		end
		local TextLabel = Instance.new("TextLabel")
		TextLabel.Parent = game:GetService("Players").PlayerGui.Default
		TextLabel.Text = player.Name .. " has nuked the server!"			
		TextLabel.FontFace =  Font.fromName("FredokaOne",Enum.FontWeight.Medium,Enum.FontStyle.Normal)
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)

		TextLabel.Visible = true
		
		Granted = true
		
		wait(5)

		TextLabel.Visible = false

	elseif receiptInfo.ProductId == 1660053718 then
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				p.PlayerGui.Default.Blinded.Visible = true
			end
		end

		local TextLabel = Instance.new("TextLabel")
		TextLabel.Text = player.Name .. " has blinded everyone for 60s!"
		TextLabel.Font = Enum.Font.FredokaOne
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)
		TextLabel.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Default
		TextLabel.Visible = true
		
		Granted = true
		
		wait(55)

		TextLabel:Destroy()
		for _, p in pairs(game.Players:GetPlayers()) do
			p.PlayerGui.Default.Blinded.Visible = false
		end

	elseif receiptInfo.ProductId == 1660090129 then
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				if p.Character and p.Character:FindFirstChild("HumanoidRootPart") then
					local Humanoid = p.Character.Humanoid
					Humanoid.WalkSpeed = Humanoid.WalkSpeed / 2
				end
			end
		end
		local TextLabel = Instance.new("TextLabel")
		TextLabel.Parent = game:GetService("Players"):WaitForChild("PlayerGui"):WaitForChild("Default")
		TextLabel.Text = player.Name .. " has slowed everyone down for 60s!"
		TextLabel.FontFace = Font.fromName("FredokaOne")
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)

		TextLabel.Visible = true
		
		Granted = true
		
		wait(5)

		TextLabel.Visible = false
		TextLabel:Destroy()
		
		wait (55)
		
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				if p.Character and p.Character:FindFirstChild("HumanoidRootPart") then
					local Humanoid = p.Character.Humanoid

					Humanoid.WalkSpeed = 16
				end
			end
		end
	elseif receiptInfo.ProductId == 1660098210 then
		for _, p in pairs(game.Players:GetPlayers()) do
			if p.Character and p.Character:FindFirstChild("HumanoidRootPart") then
				local Humanoid = p.Character.Humanoid
				Humanoid.WalkSpeed = Humanoid.WalkSpeed * 2
			end
		end
		local TextLabel = Instance.new("TextLabel")
		TextLabel.Parent = game:GetService("Players"):WaitForChild("PlayerGui"):WaitForChild("Default")
		TextLabel.Text = player.Name .. " has sped everyone up for 60s!"
		TextLabel.FontFace = Font.fromName("FredokaOne")
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)

		TextLabel.Visible = true

		wait(5)

		TextLabel.Visible = false
		TextLabel:Destroy()
		Granted = true
		
		for _, p in pairs(game.Players:GetPlayers()) do
			if p ~= player then
				if p.Character and p.Character:FindFirstChild("HumanoidRootPart") then
					local Humanoid = p.Character.Humanoid

					Humanoid.WalkSpeed = 16
				end
			end
		end
		
	elseif receiptInfo.ProductId == 1660096890 then
		game.Workspace.Gravity = 196.2 / 6
		local TextLabel = Instance.new("TextLabel")
		TextLabel.Parent = game:GetService("Players"):WaitForChild("PlayerGui"):WaitForChild("Default")
		TextLabel.Text = player.Name .. " has reduced the gravity for 60s!"
		TextLabel.FontFace = Font.fromName("FredokaOne")
		TextLabel.TextScaled = true
		TextLabel.Size = UDim2.new(0.25, 0, 0.25, 0)
		TextLabel.Position = UDim2.new(0.375, 0, 0.386, 0)
		TextLabel.TextColor3 = Color3.new(158, 0, 0)
		TextLabel.Visible = true
			
		Granted = true
			
		wait(5)

		TextLabel.Visible = false
		TextLabel:Destroy()
	end
	
	-- DONATIONS
	
	local plr = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)

	if receiptInfo.ProductId == 1660201418 then
		addDonation(plr, 25)
	end

	if receiptInfo.ProductId == 1660202093 then
		addDonation(plr, 50)
	end

	if receiptInfo.ProductId == 1660202088 then
		addDonation(plr, 100)
	end

	if receiptInfo.ProductId == 1660202092 then
		addDonation(plr, 250)
	end

	if receiptInfo.ProductId == 1660202090 then
		addDonation(plr, 500)
	end

	if receiptInfo.ProductId == 1660202091 then
		addDonation(plr, 1000)
	end

	if receiptInfo.ProductId == 1660202087 then
		addDonation(plr, 5000)
	end

	if receiptInfo.ProductId == 1660202089 then
		addDonation(plr, 10000)
	end
	
	if receiptInfo.ProductId == 1658851386 then
		-- Gérer l'achat de VIP
		-- Ajoutez ici le code pour gérer l'achat de VIP
	end
	if receiptInfo.ProductId == 265700950 and player then
		local Gear = game.ServerStorage.Gear:FindFirstChild("Speed Coil")
		if Gear then
			local gearClone = Gear:Clone()
			gearClone.Parent = player.Backpack
		end
	end
	if receiptInfo.ProductId == 265701706 and player then
		local Gear = game.ServerStorage.Gear:FindFirstChild("Gravity Coil")
		if Gear then
			local gearClone = Gear:Clone()
			gearClone.Parent = player.Backpack
		end
	end
	if receiptInfo.ProductId == 5144165381 and player then
		local Gear = game.ServerStorage.Gear:FindFirstChild("Fusion Coil")
		if Gear then
			local gearClone = Gear:Clone()
			gearClone.Parent = player.Backpack
		end
	end
	
	if Granted == true then
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

You’re only supposed to have one process receipt function in your game in exactly one script.

I would store the functions in a dictionary with a key being the receipt ID. then just call the dictionary[id]()

I personally would further offload them into module scripts, but that’s just me.

local receiptFunctions = {
    [114638] = increaseRunSpeed --the name of a function from earlier in the script. I personally would go further and put this in a module script though.
}

processReceipt(rinfo)
    return receiptFunctions[recieptID]
end

This way each function only can effect itself internally and each function can return its own answer.

1 Like

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