Skip Stage not working

For some reason it won’t reach anything within the local function.

Here’s the entire script for reference:

local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("data_01")
local marketplaceService = game:GetService("MarketplaceService")

local camera = workspace.CurrentCamera

local cpFolder = game.Workspace:WaitForChild("Checkpoints")


game.Players.PlayerAdded:Connect(function(player)
	local key = "player-" .. player.UserId

	local GetSave = DataStore:GetAsync(key) -- check for existing data 

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local hidden = Instance.new("Folder", player)
	hidden.Name = "hidden"

	local checkpoint = Instance.new("IntValue", leaderstats)
	checkpoint.Name = "Stage"

	local spawnpoint = Instance.new("IntValue", hidden)
	spawnpoint.Name = "SpawnPoint"

	if GetSave then
		checkpoint.Value = GetSave
		print("Data Loaded For " .. player.Name)
	else
		checkpoint.Value = 1
		print("New Data Created For " .. player.Name)
	end
		
	local Character = player.Character
	--local respawn2 = cpFolder[player.leaderstats.Stage.Value]
	local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
	Character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)

	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.hidden.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	end)

end)

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 < 192 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

game.Players.PlayerRemoving:Connect(function(player)
	local key = "player-" .. player.UserId
	
	DataStore:SetAsync(key, player.leaderstats.Stage.Value)
	
	print("Data Successfully Saved For " .. player.Name)
end)

Are you saying that if it’s greater than 192 they can skip the stage?

1 Like

Oh my I should of seen this earlier yes thats exactly what the problem is

That’s the less than symbol, right? I get confused sometimes but I double checked and I saw that it was. :laughing:

No worries lol, what you are saying in this line is if it’s less than 192, they can’t skip, you need to flip it to be like this:

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

It’s not working, no errors. Just to make sure we’re on the right page here’s the script. I tried debugging this with print statements before, though none of them printed.

local function processReceipt(receiptInfo)
	local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then	
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	if player.leaderstats.Stage.Value < 192 then
		player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
		player:LoadCharacter()
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	return Enum.ProductPurchaseDecision.PurchaseGranted
end
1 Like

Is it not printing in your function at all?

1 Like

Yeah it seems as if nothing prints in that local function. I tried this:

local function processReceipt(receiptInfo)
	print("Hi1!")
	local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	print("Hi2!")
	if not player then	
		print("Hi3!")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	print("Hi4!")
	if player.leaderstats.Stage.Value < 192 then
		print("Hi!5")
		player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
		print("Hi!6")
		player:LoadCharacter()
		print("Hi!7")
	else
		print("Hi!8")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

marketplaceService.ProcessReceipt = processReceipt

Try making a separate script in server script service as it works perfectly fine separated

https://gyazo.com/c5b179e4fde8a585c2c183ff9baa4552

1 Like

It seems that you haven’t called the function, am I correct or did you?

1 Like

I tried placing one now:

local marketplaceService = game:GetService("MarketplaceService")

local function processReceipt(receiptInfo)

	local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then	
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	if player.leaderstats.Stage.Value < 192 then
		player.leaderstats.Stage.Value = player.leaderstats.Stage.Value + 1
		player:LoadCharacter()
	else
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	return Enum.ProductPurchaseDecision.PurchaseGranted
end

marketplaceService.ProcessReceipt = processReceipt
marketplaceService:Connect(processReceipt())

Got this error:
ServerScriptService.Library.Datastore:55: attempt to index nil with ‘PlayerId’ - Server - Datastore:55

Which line is having the error?

local player = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)

I did, still didn’t work unfortunately.

Remove the .playerid, does that fix your issue?

Pretty sure that’d break the argument since :GetPlayerByUserId

Your parameter does the job, it’s getting the id of the player from your parameter, i don’t think you’d want to get the id from an id…?

I tried got this error: Argument 1 missing or nil
also it errors that line

Changed the structure of the script through a tutorial, still doesn’t print 2 but only prints 1.

local marketplaceService = game:GetService("MarketplaceService")
print("1")
marketplaceService.ProcessReceipt = function(receiptInfo)
	print("2")
	local players = game.Players:GetPlayers()
	local done = 0

	for i=1,#players do
		if players[i].userId == receiptInfo.PlayerId then
			if receiptInfo.ProductId == 1086221671 and done == 0 then
				done = 1
				players[i].leaderstats.Stage.Value = players[i].leaderstats.Stage.Value + 1
				players[i].TeleportedStage.Value = players[i].leaderstats.Stage.Value
				players[i].Character:MoveTo(game.Workspace.Stages:FindFirstChild(players[i].TeleportedStage.Value).Position)
				done = 0
			end
		end
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted	
end
local player = game.Players.LocalPlayer
local productId = 1086221671
script.Parent.MouseButton1Click:Connect(function()
	script.Parent.SkipStageScript.Click:Play()
	game:GetService("MarketplaceService"):PromptProductPurchase(player, productId)
end)