Skip Stage Purchase

I have an obby and I want to make a developer product which skips one stage of the player. I am getting this error. I understand it but do not know how to fix it.
Capture

The script for the purchase:

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





local done = 0

MarketplaceService.ProcessReceipt = function(receiptInfo)
local players = game.Players:GetPlayers()

	
local function SkipStage (player)

			if receiptInfo.ProductId == 1002628730 and done == 0 then
				done = 1
				player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
				player.Character.Humanoid.Health = 0
				done = 0
			end	
	end
	SkipStage()
	end	

The prompt inside the button -

local productId = 1002628730 --paste you product ID in here
local player = game.Players.LocalPlayer

debounce = false

script.Parent.MouseButton1Click:connect(function()
	
if not debounce then
	debounce = true
	
	game:GetService("MarketplaceService"):PromptProductPurchase(player, productId)
end
end)
wait(15)
debounce = false
1 Like

Someone plz answer my question, I have been working on this for so long! :sob:

Can you spawn in test mode and show me a picture of your player object in the explorer?

1 Like

The error message says Attempt to index nil with 'leaderstats'

This means that leaderstats does not exist in the path you are trying to index it via which appears to be the player.

If you are already creating the leaderstats somewhere else and are firing this function almost instantly then you may want to add a WaitForChild()

If you are not creating the leaderstats folder you need to do that when the player joins.

I would recommend you learn how to read error messages because it can help you a lot in debugging and make you an overall better developer.

1 Like

You may need this script.

game.Players.PlayerAdded:Connect(function(plr)
local fold = Instance.new("Folder", plr)
fold.Name = "leaderstats"
local value = Instance.new("NumberValue", fold)
value.Name = "Stage"
end)
2 Likes
local RunService = game:GetService("RunService")
MarketplaceService = game:GetService("MarketplaceService")





local done = 0

MarketplaceService.ProcessReceipt = function(receiptInfo)
local players = game.Players:GetPlayers()

	
local function SkipStage (player)

			if receiptInfo.ProductId == 1002628730 and done == 0 then
				done = 1
				player:WaitForChild("leaderstats").Stage.Value =player:WaitForChild("leaderstats").Stage.Value + 1
				player.Character.Humanoid.Health = 0
				done = 0
			end	
	end
	SkipStage()
	end	
	```
It says attempt to index nil with waitforchild.

For testing purpouses put my script on top of yours so they are in the same file and mine will execute first, It would also be helpful if you told me when your script gets executed.

1 Like

i have your script part in my game already

Put them in one script so it should look like this

game.Players.PlayerAdded:Connect(function(plr)
local fold = Instance.new("Folder", plr)
fold.Name = "leaderstats"
local value = Instance.new("NumberValue", fold)
value.Name = "Stage"
end)
local done = 0

MarketplaceService.ProcessReceipt = function(receiptInfo)
local players = game.Players:GetPlayers()

	
local function SkipStage (player)

			if receiptInfo.ProductId == 1002628730 and done == 0 then
				done = 1
				player:WaitForChild("leaderstats").Stage.Value =player:WaitForChild("leaderstats").Stage.Value + 1
				player.Character.Humanoid.Health = 0
				done = 0
			end	
	end
	SkipStage()
	end	
	```
1 Like

Also if the error says attempt to index nil with waitforchild it is because somewhere WaitForChild is not capitalized correctly It should look like this WaitForChild

2 Likes

So first of all you are calling the function SkipStage() without the player argument, so you should at least send the player as an argument otherwise this will be nil. Besides that you also make a variable ‘players’ which is never used. A quick fix would look like this (I created a player argument that is being sent as argument to the function):

MarketplaceService.ProcessReceipt = function(receiptInfo)
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)

	
local function SkipStage (player)

			if receiptInfo.ProductId == 1002628730 and done == 0 then
				done = 1
				player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
				player.Character.Humanoid.Health = 0
				done = 0
			end	
	end
	SkipStage(player)
	end