Have game pass check if player has tool & if not it will give them the tool?

  1. What do you want to achieve?
    I want the script to check if the player has the game pass when joining the game, then check if the player has the tool & if the player doesn’t have the tool … the tool will then be cloned into their backpack!

  2. What is the issue?
    I don’t think my entire script is running since it’s only printing one of the prints.
    Screen Shot 2022-08-14 at 4.47.12 PM

The script is in ServerScriptService.
Here’s the script:

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

local gamePassID = 73240559 

local function onPlayerAdded(player)

	local hasPass = false

	-- 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")
		if player.Backpack:FindFirstChild("Hello Kitty's Wand") then return end--Makes Sure The Player Doesn't Have Tool
		
		print(player.Name .. "player already has Hello Kitty's Wand" .. gamePassID)
		
		-- Assign this player the ability or bonus related to the game pass
		local Tool = game.ServerStorage.Shop["Hello Kitty's Wand"]:Clone()
		Tool.Parent = player.Backpack
		
		print(player.Name .. "Hello Kitty's Wand given to player" .. gamePassID)
	end
end

-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

Is there something wrong with the script that’s causing it not to work correctly?

Thank you for your help :slight_smile: :heart:

3 Likes

Doesn’t seem like theres any problems with the script.
Put a print check in the if statement that checks if the tool is already in the players backpack. Im sure thats why the whole function is not running fully.

2 Likes

Thank you – I thought I’ve done that here in the script:

Unless I’ve done it wrong? I’m kind of new to scripting so :sweat_smile:

1 Like

You put the print after the if statement. It needs to be in the scope of it (inside it) before the “return”

2 Likes

Ahhh, I see! Like this?

if player.Backpack:FindFirstChild("Hello Kitty's Wand") then 
print(player.Name .. "player already has Hello Kitty's Wand" .. gamePassID)
return end--Makes Sure The Player Doesn't Have Tool		
2 Likes

Yes like that. Run a test and see if it prints

3 Likes

Screen Shot 2022-08-14 at 5.24.58 PM
This is what prints after altering the script; however, I believe that the script is working properly because it isn’t giving me multiples of the same tool … which is what I want to happen.

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

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

local function onPlayerAdded(player)

	local hasPass = false

	-- 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")
		if player.Backpack:FindFirstChild("Hello Kitty's Wand") then 
			print(player.Name .. "already has Hello Kitty's Wand " .. gamePassID)
			return end--Makes Sure The Player Doesn't Have Tool
		
		-- Assign this player the ability or bonus related to the game pass
		local Tool = game.ServerStorage.Shop["Hello Kitty's Wand"]:Clone()
		Tool.Parent = player.Backpack
		wait (.1)
		print(player.Name .. " has been given Hello Kitty's Wand " .. gamePassID)
	end
end

-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

I made a gamepass compilation that includes Tools for having a gamepass. It might help.

If you want to get a specific solution to your problem, guys above sent solutions.

2 Likes

Thanks for this! Your post will definitely be a useful resource in the future!

I have an Inventory DataStore so I don’t want my tool to be cloned each time the player joins the game since then they’d have like 6 wands when they really only need one. It makes it a bit tricky but I think my script works … I hope!

1 Like