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.
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
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.
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
```
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
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