Skip Stage help

Good afternoon, I need help with a script please. I’m still new to scripting, but I finally managed to make a skip stage button in my obby.

However, there’s an issue. Once I get to the last stage, if I skip again, I get stuck in an endless loop of dying. I understand why that happens, I just need help adding some functionality to prevent it from happening. (Maybe a temporary disable button?)

I will add my scripts in, thanks in advance.

This script is in ServerScriptService

MarketPlaceService = game:GetService("MarketplaceService")

MarketPlaceService.ProcessReceipt = function(receiptInfo)
	local players = game.Players:GetPlayers()
	local node = "Node"
	local done = 0
	
	for i = 1, #players do
		if players[i].UserId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == 1109847551 and done == 0 then
				done = 1
				players[i].leaderstats[node].Value = players[i].leaderstats[node].Value + 1
				players[i].Character.Humanoid.Health = 0
				done = 0
				
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

And this script is behind my gui button

local productId = 1109847551 -- Change if needed
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Up:Connect(function()
	game:GetService("MarketplaceService"):PromptProductPurchase(player, productId)
	
end)

Why are you using integers instead of boolean for done?
You should add an if statement that disables the button once the player reaches the last stage.

1 Like

I thought you could use either 0/1 or true/false?

Yes, but it’s easier to just use true and false, so we know it shouldn’t be anything except those 2 values.

1 Like

Pretty simple; in the if condition add this logic.

if players[i].leaderstats[node].Value == MAX_VALUE then return false end
1 Like

Also, to make your code more readable, define constants at the top of your script, and use += when adding to that same value.

local productid = 1109847551
players[i].leaderstats[node].Value += 1
1 Like

A way more optimized script wud be

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

MarketPlaceService.ProcessReceipt = function(receiptInfo)
	local node = "Node"

	for _, player : Player in ipairs(Players:GetPlayers()) do
		if player.UserId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == 1109847551 then
                if player.leaderstats[node] and not player.leaderstats[node].Value == MAX_VAL then -- Replace MAX_VAL with ur max stage (number)
				    player.leaderstats[node].Value += 1
				    player.Character.Humanoid.Health = 0

                    return Enum.ProductPurchaseDecision.PurchaseGranted
                else
                    return false
                end
			end
		end
	end

    return false
end