Gamepass script has been giving players everything for free, what have I done wrong?

Sorry if I’ve formatted all this wrong, I haven’t posted a topic here before and am not sure how.

I’ve had a script that I got from a YouTube tutorial on my game for months now to give the player the item after buying the gamepass by moving it from ServerStorage, since it gives the owner the gamepass regardless I just assumed it was working correctly since it seemed to work from my POV in Studio.
Issue is, I now just played via a new Alt account and have now noticed its giving players every item regardless of if they have the gamepass or not, I have no idea what I’ve done wrong here or how to correct it
(No wonder no one bought any gamepasses, theyve had them free this whole time >.<)

Please note I have no idea how to script so broken down explanations are needed, I only use Youtube tutorials for everything
This is the script I used:

local VipPlayers = {"nill"} 
local ToolName = {"Apple"} -- put the name of the tool and it needs to be in ServerStorage

local function FindPlayer(Plr)
	for Num, Pler in pairs(VipPlayers) do
		if Pler == Plr then
			return true
		end
	end
end

game.Players.PlayerAdded:connect(function(Player)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(Player.UserId, Id) or FindPlayer(Player.Name) then 
		Player.CharacterAdded:Connect(function(character)
			for Num, Tool in pairs(ToolName) do
				if game:GetService("ServerStorage"):FindFirstChild(Tool) then
					game:GetService("ServerStorage")[Tool]:Clone().Parent = Player.Backpack
				end
			end
		end)

		for Num, Tool in pairs(ToolName) do
			if Player.Backpack:FindFirstChild(Tool) == nil then
				if game:GetService("ServerStorage"):FindFirstChild(Tool) then
					game:GetService("ServerStorage")[Tool]:Clone().Parent = Player.Backpack
				end
			end
		end
	end
end)

local MarketplaceService=game:GetService("MarketplaceService")
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player,id)
	for Num, Tool in pairs(ToolName) do
		if game:GetService("ServerStorage"):FindFirstChild(Tool) then
			game:GetService("ServerStorage")[Tool]:Clone().Parent = Player.Backpack
		end
	end
end)```

In the prompt purchase gamepass finished I’m pretty sure you have to detect if they buy it cause that fires when the prompt finishes

1 Like

You can add another parameter and call it wasPurchased and if it’s true grant them the tool

I get that concept, but I don’t think that’s what happened here as its happening even if a prompt is never opened, It doesn’t happen right away either, it happens about 10 mins into gameplay, thats why I’m so confused. I’ve triple checked everything’s in the correct folders, named and numbered correctly etc and they certainly are

In the end, I’ve just used a different script, This new one seems to be working fine now

local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage") 
local GamepassID = 0000000 
local ToolsFolder = ReplicatedStorage:FindFirstChild("GamepassTools")
local ToolName = "name"

Players.PlayerAdded:Connect(function(player)
 player.CharacterAdded:Connect(function(character)
  if GamepassID ~= 0 and MarketplaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
   if ToolsFolder then
    local tool = ToolsFolder:FindFirstChild(ToolName)
    if tool and tool:IsA("Tool") then
     local clone = tool:Clone()
     clone.Parent = player.Backpack
    end
   end
  end
 end)
end)```

the script is detecting whether the user owns the gamepass OR whether the player exists, which is always true

You can check out the documentation, it contain pre-made code to handle gamepasses which could help.


Local Script

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

local Player = PlayerService.LocalPlayer

local function PromptGamepassPurchase(GamepassID)
	local GamepassOwned = false

	local Success, Message = pcall(function()
		GamepassOwned = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID)
	end)

	if Success and not GamepassOwned then
		MarketplaceService:PromptGamePassPurchase(Player, GamepassID)
	end
end

--When clicking a button or whatever
PromptGamepassPurchase(0000000)

Server Script

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

local function GiveGamepassBenefits(Player, GamepassID)
	if GamepassID == 0000000 then
		-- Give the gamepass bonus to player
	elseif GamepassID == 1111111 then
		-- Give the gamepass bonus to player
	end
end

PlayerService.PlayerAdded:Connect(function(NewPlayer)
	local GamepassOwned = {}

	local Success, Message = pcall(function()
		GamepassOwned["0000000"] = MarketplaceService:UserOwnsGamePassAsync(NewPlayer.UserId, 0000000)
		GamepassOwned["1111111"] = MarketplaceService:UserOwnsGamePassAsync(NewPlayer.UserId, 1111111)
	end)

	if Success then
		for GamepassID, IsOwned in GamepassOwned do
			if IsOwned == true then
				GiveGamepassBenefits(NewPlayer, tonumber(GamepassID))
			end
		end
	end
end)

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(Player, PurchasedGamepassID, PurchaseSuccess)
	if PurchaseSuccess then
		GiveGamepassBenefits(Player, PurchasedGamepassID)
	end
end)