Part Spawner Script Not Working When I Increase The Amount Of Parts

Recently I’ve been working on a script that spawns parts when a developer product is purchased, and it works fine when around 10 parts are spawned, but when I increase the parts to a higher value, it fails to work. I’m not good at scripting so I’ve been making the script in a weird way that instead of spawning 10 parts all at once by doing something like “Spawn 10x Pats”, it spawns 1 part and i duplicate that part of the script until i get the desired amount. It’s definitely not practical so i assume that’s why it doesn’t work when the value is increased, so i need help compressing all of that code into just a couple lines, so that i could just change a number depending on how many parts i want to be added instead of duplicating lines of code if that makes sense

Here is the code

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

local DEVELOPER_PRODUCT_ID = 3335469046

MarketplaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == DEVELOPER_PRODUCT_ID then
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
		local part = Instance.new("Part")
		part.Size = Vector3.new(1, 1, 1)
		part.Position = Vector3.new(0, 100, 0)
		part.Anchored = false
		part.BrickColor = BrickColor.new("Cork")
		part.Parent = Workspace
		
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

The scripts are in ServerScriptService

Use a loop :3

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

local DEVELOPER_PRODUCT_ID = 3335469046
local NUMBER_OF_PARTS_TO_SPAWN = 10

MarketplaceService.ProcessReceipt = function(receiptInfo)
    if receiptInfo.ProductId == DEVELOPER_PRODUCT_ID then
        for i = 1, NUMBER_OF_PARTS_TO_SPAWN do
            local part = Instance.new("Part")
            part.Size = Vector3.new(1, 1, 1)
            part.Position = Vector3.new(0, 100, 0)
            part.Anchored = false
            part.BrickColor = BrickColor.new("Cork")
            part.Parent = Workspace
        end
        
        -- confirm that the purchase was successful 
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
    
    -- PID doesn't match
    return Enum.ProductPurchaseDecision.NotProcessedYet
end

thank you, ill test to see if it works :slight_smile:

for some reason it works sometimes and sometimes it doesn’t.

Might be another script. If I remember correctly you could try
Using only one script with ProcessReceipt
Try placing it in ServerScriptService

not sure if this is what you meant, but i tried using only one script, this is it now:

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

local DEVELOPER_PRODUCT_ID1 = 3335469046
local NUMBER_OF_PARTS_TO_SPAWN1 = 9

MarketplaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == DEVELOPER_PRODUCT_ID1 then
		for i = 1, NUMBER_OF_PARTS_TO_SPAWN1 do
			local part = Instance.new("Part")
			part.Size = Vector3.new(1, 1, 1)
			part.Position = Vector3.new(0, 100, 0)
			part.Anchored = false
			part.BrickColor = BrickColor.new("Cork")
			part.Parent = Workspace
		end

		-- confirm that the purchase was successful 
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	-- PID doesn't match
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

local DEVELOPER_PRODUCT_ID2 = 3336411649
local NUMBER_OF_PARTS_TO_SPAWN2 = 50

MarketplaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == DEVELOPER_PRODUCT_ID2 then
		for i = 1, NUMBER_OF_PARTS_TO_SPAWN2 do
			local part = Instance.new("Part")
			part.Size = Vector3.new(1, 1, 1)
			part.Position = Vector3.new(0, 100, 0)
			part.Anchored = false
			part.BrickColor = BrickColor.new("Cork")
			part.Parent = Workspace
		end

		-- confirm that the purchase was successful 
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	-- PID doesn't match
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

but it still doesn’t work. when i try to spawn 50 pieces it works, but when i try to spawn 10 it doesn’t

Yup. I think I didn’t explain it clearly enough. when you create two ProcessReceipt functions the second one overwrites the first one.

local PRODUCTS = {
	-- Product ID 1
	[3335469046] = {
		PartsToSpawn = 9
	},
	-- Product ID 2
	[3336411649] = {
		PartsToSpawn = 50
	}
}
-- in ProcessReceipt function
local productInfo = PRODUCTS[receiptInfo.ProductId]

if productInfo then
	-- everything stays the same except for the loop
	for i = 1, productInfo.PartsToSpawn do

thanks for letting me know, but sorry for asking, I’m not good at coding, but where would I put those in the script?

At the top!! You define it before the part where you use it!!

I’m so sorry I’m definitely doing this wrong :sob:

local PRODUCTS = {
	-- Product ID 1
	[3335469046] = {
		PartsToSpawn = 9
	},
	-- Product ID 2
	[3336411649] = {
		PartsToSpawn = 50
	}
}
-- in ProcessReceipt function
local productInfo = PRODUCTS[receiptInfo.ProductId]

if productInfo then
	-- everything stays the same except for the loop
	for i = 1, productInfo.PartsToSpawn do
local MarketplaceService = game:GetService("MarketplaceService")
local Workspace = game:GetService("Workspace")

local DEVELOPER_PRODUCT_ID1 = 3335469046
local NUMBER_OF_PARTS_TO_SPAWN1 = 9

MarketplaceService.ProcessReceipt = function(receiptInfo)
	if receiptInfo.ProductId == DEVELOPER_PRODUCT_ID1 then
		for i = 1, NUMBER_OF_PARTS_TO_SPAWN1 do
			local part = Instance.new("Part")
			part.Size = Vector3.new(1, 1, 1)
			part.Position = Vector3.new(0, 100, 0)
			part.Anchored = false
			part.BrickColor = BrickColor.new("Cork")
			part.Parent = Workspace
		end

		-- confirm that the purchase was successful 
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	-- PID doesn't match
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

happens to the best of us

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

local PRODUCTS = {
	[3335469046] = {
		PartsToSpawn = 9
	},
	[3336411649] = {
		PartsToSpawn = 50
	}
}

MarketplaceService.ProcessReceipt = function(receiptInfo)
	local productInfo = PRODUCTS[receiptInfo.ProductId]

	if productInfo then
		for i = 1, productInfo.PartsToSpawn do
			local part = Instance.new("Part")
			part.Size = Vector3.new(1, 1, 1)
			part.Position = Vector3.new(0, 100, 0)
			part.Anchored = false
			part.BrickColor = BrickColor.new("Cork")
			part.Parent = Workspace
		end
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	return Enum.ProductPurchaseDecision.NotProcessedYet
end
1 Like

still doesn’t work for some reason :man_shrugging:

i figured it out just changed a few things up and got some help from a friend

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