[HELP!] Need help dividing elseif statement (DEVELOPER PRODUCTS)

Hello! I created some elseif statements that are supposed to give you the leaderstat value of coins when you purchase a developer product. Also in the script, I added another instance where it will reset you and add another point to your stage value. Everything works, but the problem is that I need to divide the functions so when you purchase the coin developer products, that the script won’t add any points to your stage value. Is there a way to do this? Thanks!

function processReceipt(info)
	local plr = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)
	
	if plr then
	if info.ProductId == 1728120559 then
		plr.Character.Humanoid.Health = 0
		local stats = plr:FindFirstChild("leaderstats")
		local changeStage = stats and stats:FindFirstChild("Stage")
		if changeStage then
			changeStage.Value += 1
			return true

		end
	elseif info.ProductId == 1729065465 then
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value +25
			plr.Character.Humanoid.Health = 100
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value +1
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value -1
			game.ReplicatedStorage.PurchaseGood:FireClient(plr)
			return Enum.ProductPurchaseDecision.PurchaseGranted
		elseif info.ProductId == 1729065464 then
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value +50
			plr.Character.Humanoid.Health = 100
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value +1
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value -1
			game.ReplicatedStorage.PurchaseGood:FireClient(plr)
			return Enum.ProductPurchaseDecision.PurchaseGranted
		elseif info.ProductId == 1729065463 then
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value +125
			plr.Character.Humanoid.Health = 100
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value +1
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value -1
			game.ReplicatedStorage.PurchaseGood:FireClient(plr)
			return Enum.ProductPurchaseDecision.PurchaseGranted
		elseif info.ProductId == 1729065466 then
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value +250
			plr.Character.Humanoid.Health = 100
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value +1
			plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value -1
			game.ReplicatedStorage.PurchaseGood:FireClient(plr)
			return Enum.ProductPurchaseDecision.PurchaseGranted
		
		end
		else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt

I looked around on the devforum, and I decided that posting would be the best option, because there’s no switch case system in lua.

1 Like

Is this what you are asking for?

function processReceipt(info)
	local plr = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)

	if plr then
		local coinsIncrease = 0
		
		if info.ProductId == 1728120559 then
			plr.Character.Humanoid.Health = 0
			local stats = plr:FindFirstChild("leaderstats")
			local changeStage = stats and stats:FindFirstChild("Stage")
			if changeStage then
				changeStage.Value += 1
				return true
			else
				-- I don't know if you want to return here or not.
			end
		elseif info.ProductId == 1729065465 then
			coinsIncrease += 25
		elseif info.ProductId == 1729065464 then
			coinsIncrease += 50
		elseif info.ProductId == 1729065463 then
			coinsIncrease += 125
		elseif info.ProductId == 1729065466 then
			coinsIncrease += 250
		end
		
		plr.leaderstats.Coins.Value += coinsIncrease
		plr.Character.Humanoid.Health = 100
		plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value +1
		plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value -1
		game.ReplicatedStorage.PurchaseGood:FireClient(plr)
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt
2 Likes

It seems that after you purchase the skip stage, then when you purchase a coin developer product, it will still add a stage point. Maybe blocking “changeStage” when you purchase the coin product could work. Is there a way to do that?

1 Like

I don’t know what you want or are asking. If you want to return always when buying a skipStage, add a return on the last line of the changeStage block.

2 Likes

Thanks for helping! I’ll try to fix it. Btw, what I meant is that when you buy any coin developer product, it will also increase the leaderstat value for Stages, which means it probably runs the script from the skip stage part that increases that leaderstat.

1 Like

Since the differences between products are quite small, you should use a dictionary to identify what needs to be changed.

ProductEffects = {
	--ProductId = {Coins,Skip}
	1728120559 = {Coins = 0,Skip = true};
	1729065465 = {Coins = 25,Skip = false};
	1729065464 = {Coins = 50,Skip = false};
	1729065463 = {Coins = 125,Skip = false};
	1729065466 = {Coins = 250,Skip = false};
}
function processReceipt(info)
	local plr = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)
	
	if plr then
		if ProductEffects[info.ProductId] then

			local SelectedProduct = ProductEffects[info.ProductId]
			if SelectedProduct.Coins > 0 then
				plr.leaderstats.Coins.Value += SelectedProduct.Coins
			end
			if SelectedProduct.Skip == true then
				plr.Character.Humanoid.Health = 0
				local stats = plr:FindFirstChild("leaderstats")
				local changeStage = stats and stats:FindFirstChild("Stage")
				if changeStage then
					changeStage.Value += 1
					return true

				else
					--//Probably want to return something here.
				end
			else
				plr.Character.Humanoid.Health = 100
			end

			game.ReplicatedStorage.PurchaseGood:FireClient(plr)
			return Enum.ProductPurchaseDecision.PurchaseGranted
		
		end --//You probably want to do something if the sent ProductID is bad.
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt

game:GetService("MarketplaceService").ProcessReceipt = processReceipt

Also, what’s the point of this piece of code? Seems redundant.

plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value +1
plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value -1
2 Likes

Does this help?

function processReceipt(info)
	local plr = game:GetService("Players"):GetPlayerByUserId(info.PlayerId)
	
	if plr then
		if info.ProductId == 1728120559 then
			-- Logic for product with ID 1728120559
			plr.Character.Humanoid.Health = 0
			local stats = plr:FindFirstChild("leaderstats")
			local changeStage = stats and stats:FindFirstChild("Stage")
			if changeStage then
				changeStage.Value += 1
				return true
			end
		elseif info.ProductId == 1729065465 then
			-- Logic for product with ID 1729065465
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 25
			plr.Character.Humanoid.Health = 100
		elseif info.ProductId == 1729065464 then
			-- Logic for product with ID 1729065464
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 50
			plr.Character.Humanoid.Health = 100
		elseif info.ProductId == 1729065463 then
			-- Logic for product with ID 1729065463
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 125
			plr.Character.Humanoid.Health = 100
		elseif info.ProductId == 1729065466 then
			-- Logic for product with ID 1729065466
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 250
			plr.Character.Humanoid.Health = 100
		end

		-- Common logic for all coin products
		plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value + 1
		plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value - 1
		game.ReplicatedStorage.PurchaseGood:FireClient(plr)
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
end

game:GetService("MarketplaceService").ProcessReceipt = processReceipt
2 Likes

I forgot to delete that part. Sorry! It was an attempt to balance the values for the stage.

Thanks for helping! I edited that script a bit, and I saw that if I deleted the return true, then it would work. Thanks!

1 Like

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