Gamepass Tool Giver Won't Work

What do you want to achieve?
Here is the step by step process I’m trying to achieve:

  1. I want the ClickPart to check if the player has the gamepass.
  2. Check if the player already has the item in their inventory, if so the ‘Repeat’ ScreenGUI will appear on their screen.
  3. If the player owns the gamepass but doesn’t have the item in their inventory, the item will be cloned into their backpack & the ‘Success’ ScreenGUI will appear. (If they try to purchase the item again, they won’t be able to since it’s already in their backpack).
  4. If the player doesn’t own the gamepass then the ‘Failed’ ScreenGUI will appear & the item won’t be put into their backpack.

What is the issue?

The ‘Repeat’ ScreenGUI appears but the ‘Failed’ & ‘Success’ ScreenGUIs do not.

  1. What solutions have you tried so far?

I’ve tried re-writing my code many times & it still won’t work the way I want it to. Is there something wrong with my code? I’m not getting any output errors but it’s not working the way I want it to.

Here’s the code:

--local RequiredGold = script.Parent.Parent.Parent.Price.Value
local player = game.Players.LocalPlayer
local tool = game.ServerStorage.Shop["Hello Kitty's Wand"]

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

local gamePassID = 73240559  -- Change this to your game pass ID

local hasPass = false
local debounce = false
		
script.Parent.MouseClick:Connect(function(player)
	
	-- Check if the player already owns the game pass
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	-- If there's an error, issue a warning and exit the function
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
	
	if hasPass == true then
	print(player.Name .. " owns the game pass with ID " .. gamePassID)
		local Backpack = player:WaitForChild("Backpack")
		--	return end--Makes Sure The Player Doesn't Have Tool
		
	if player.Backpack:FindFirstChild("Hello Kitty's Wand") then
		print(player.Name .. "already has Hello Kitty's Wand " .. gamePassID)	
		player.PlayerGui:FindFirstChild("Repeat") 
		local Repeat = script.Repeat:clone()
		Repeat.Parent = player.PlayerGui
		wait(2)
		Repeat:Destroy()
		debounce = false
		else 
			if not debounce then -- Here is the debounce!
				debounce = true
				if hasPass == true then
					print(player.Name .. " has been given Hello Kitty's Wand " .. gamePassID)
					player.PlayerGui:FindFirstChild("Success") 
					local Success = script.Success:clone()
					Success.Parent = player.PlayerGui
					wait(2)
					tool:Clone().Parent = player.Backpack
					player.Character.Humanoid:UnequipTools()
					Success:Destroy()
				else
					if hasPass == false then
						player.PlayerGui:FindFirstChild("Failed")
						local Failed = script.Failed:clone()
						Failed.Parent = player.PlayerGui
						wait(2)
						Failed:Destroy()
					end
					debounce = false -- place is outside of the other function so it can restart the process.
				end
			end
		end
		end
	end)

Screen Shot 2023-01-10 at 5.44.25 PM

1 Like

Try doing UserOwnsGamePassAsync directly.

if MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID) then
    --do stuff
end
1 Like

That would not make any sense at all. UserOwnGamePassAsync returns boolean of if player owns gamepass or not.
Other than some function not having first letter uppercase (such as :clone() instead of :Clone()) which is technically wrong, I see no field for script to fail.
Maybe just correcting the way some function were spelled would help.

Looks like you used game.Players.LocalPlayer in a frontend script. You cannot use Localplayer unless the script is a Localscript. There is an error that would tell you this too.

Using local success, message = pcall() wouldn’t be ideal for this. You can just check if it’s true by using an if statement.

Please also use task.wait() instead of wait(). The compiler reads it much faster.

As @BloxxyLenny said, there is some functions where you start with a lowercase. So make sure you use functions that are showing up in autocorrect. If there’s no auto complete, you can tell the code what object it is using colons after you define a variable.

You can fix the localplayer issue by deleting the 2nd line of code, you wouldn’t need it anyway considering you define it in the function.

Why use

local player =  game.Players.LocalPlayer

when you already have the player variable in the function?

I dont see you using the LocalPlayer as a variable outside the function, so perhaps try removing that sience its kinda usless,

Also try printing when the Succes or Failed screen pops up so you can check when the script fails

I checked one more time and i think i found the issue, the local player and the player mentioned in the function may overlap eachother and cause the issue