How do I stop the skip stage button from working when on the last stage?

Hello, I’ve been having an issue where when you get to the last stage in the game and press skip stage, it’ll skip you to the non-existing stage. I’ve tried preventing this by stopping you from purchasing the gamepass, however it hasn’t been working.


local checkpoints = workspace:WaitForChild("Checkpoints")
local marketplaceService = game:GetService("MarketplaceService")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Levels")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local v = Instance.new("IntValue")
	v.Value = 1
	v.Name = "Stage" 
	v.Parent = leaderstats
	
	local function processReceipt(receiptInfo)
		
		local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
		if not player then	
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
		
		if receiptInfo.ProductId == 1086221671 then
			if player.leaderstats.Stage.Value == player.leaderstats.Stage.Value == 42 then
				return Enum.ProductPurchaseDecision.NotProcessedYet
				else
				player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
				player:LoadCharacter()
			end
		end
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	end
	
	marketplaceService.ProcessReceipt = processReceipt
2 Likes

Change this line of code to the following:

if player.leaderstats.Stage.Value <= 42 then
	player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
	player:LoadCharacter()
else return Enum.ProductPurchaseDecision.NotProcessedYet
end

You can use the less than or equal to (<=), less than (<), greater than or equal to (>=), or greater than (>) operators to compare numerical values.
Unsure if this is the only issue with the code, however this is the first thing I noticed.

1 Like

It doesn’t bring you to the next stage which is a plus, however it seemed to still purchase it. Which isn’t what I want just in case some buys skip stage for the non-existent level and loses robux.

Try applying the same check before you request a purchase to the player, and if it doesn’t go through then simply don’t request a purchase to that player.

1 Like

I tried this and it didn’t seem to work.

		if receiptInfo.ProductId == 1086221671 then
			if player.leaderstats.Stage.Value <= 45 then
				return Enum.ProductPurchaseDecision.NotProcessedYet
			else
				player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
				player:LoadCharacter()
			end
		end
1 Like

Could I see the rest of the code or at least the chunk where you’re using this?

1 Like

local checkpoints = workspace:WaitForChild("Checkpoints")
local marketplaceService = game:GetService("MarketplaceService")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Levels")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local v = Instance.new("IntValue")
	v.Value = 1
	v.Name = "Stage" 
	v.Parent = leaderstats
	
	local function processReceipt(receiptInfo)
		
		local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
		if not player then	
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end
		
		if receiptInfo.ProductId == 1086221671 then
			if player.leaderstats.Stage.Value <= 47 then
				return Enum.ProductPurchaseDecision.NotProcessedYet
			else
				player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
				player:LoadCharacter()
			end
		end
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
		
	end
	
	marketplaceService.ProcessReceipt = processReceipt
	
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		
		char:MoveTo(checkpoints[v.Value].Position)
		
		hum.Touched:Connect(function(hit)
			
			if hit.Parent == checkpoints and char.Humanoid.Health ~= 0 then
				if tonumber(hit.Name) == v.Value +1 then
					v.Value = v.Value +1
				end
				
			elseif hit.Parent == workspace.Kills then
				char.Humanoid.Health = 0
			end
		end)
	end)
	
	local data = plr.leaderstats.Stage.Value
	
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(plr.UserId.."-stage")
	end)
	
	if success then
		v.Value = data
		print("Player data successfully loaded!")
	else
		print("There was an error while getting your data...")
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."-stage",plr.leaderstats.Stage.Value)
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving data...")
		warn(errormessage)
	end
end)
1 Like

I meant more where you request the purchase, sorry for not clarifying.

1 Like
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	
	game:GetService("MarketplaceService"):PromptProductPurchase(player, 1086221671)
	
end)
1 Like

When clicking that,

Check if the player is on the last stage. If he’s not. Then continue the purchase.

1 Like

I think it’s best to deny the purchase prompt if the requested player doesn’t meet the specifications rather than doing it as the transaction is processing to clear up confusion. Use the check when you call the prompt.

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	if player.leaderstats.Stage.Value <= 47 then
		game:GetService("MarketplaceService"):PromptProductPurchase(player, 1086221671)
	else
		--if possible, give the client a reason why they weren't allowed to trigger a purchase
	end
end)
1 Like

Change:

if player.leaderstats.Stage.Value == player.leaderstats.Stage.Value == 42 then

To:

if v.Value == 42 then

I modified it a bit, and now it says it’s “you’re at the last stage.” you should be able to skip.

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	if player.leaderstats.Stage.Value > 48 then
		game:GetService("MarketplaceService"):PromptProductPurchase(player, 1086221671)
	else
		print("You're at the last stage.")
	end
end)
1 Like

You are checking if the player is after stage 48. > means greater than, and < means less than.

1 Like

That’s embarrassing, just fixed that up now. :flushed:

It works, now. However the only issue left is when I buy the skip stage, it won’t teleport me to the next stage.

EDIT: I know why.

Can you post the updated server script?