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
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.
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.
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
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)
local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
game:GetService("MarketplaceService"):PromptProductPurchase(player, 1086221671)
end)
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)
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)