Giving an item on touch if a player owns a gamepass

Anyone know why my script isn’t working? Nothing appears in the output.

Script:

local Players = game:GetService("Players")
local part = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")

part.Touched:Connect(function(hit)
	if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end

	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	local gamepass = 26096507

	if MarketplaceService:UserOwnsGamepassAsync(Player.UserId, gamepass) then
		local Find = Player.Backpack:FindFirstChild("WaterBottle")
		if not Find then
			local h = game.ReplicatedStorage.WaterBottle:Clone()
			h.Parent = Player:WaitForChild("Backpack")
			print("Bottle Given.")
		else MarketplaceService:PromptGamePassPurchase(gamepass)
		end	
	end
end)

Explorer:
image

Code within LocalScripts will only run if it’s a descendant of any of the following objects:

  • A Player’s Backpack , such as a child of a Tool
  • A Player’s Character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts .
  • The ReplicatedFirst service

Although you didn’t show the full hierarchy leading up to the LocalScript in the screenshot you posted, it appears as if it is not a descendant of any of the objects listed above, which means that the LocalScript’s code won’t run.

Converting that LocalScript to a Server Script should allow it to work as intended, running the function when that part is touched while also making sure that the action of giving the item to the player is replicated properly (seen by the server & other players).

It was part of a part. I changed it to a server script but I just get this error message.

It’s :UserOwnsGamePassAsync(), make sure to check the caps.

Stupid typos, I didn’t capitalize the p in gamepass. I had to get off my PC but I will try this tmr and see if it works. (Which it most like will.)

1 Like
local Players = game:GetService("Players")
local part = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")

part.Touched:Connect(function(hit)
	if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end

	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	local gamepass = 26096507

	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
		local Find = Player.Backpack:FindFirstChild("WaterBottle")
		if not Find then
			local h = game.ReplicatedStorage.WaterBottle:Clone()
			h.Parent = Player:WaitForChild("Backpack")
			print("Bottle Given.")
		else MarketplaceService:PromptGamePassPurchase(gamepass)
		end	
	end
end)

It is in a server script and this isnt working.

According to the documentation of PromptGamePassPurchase(), the player object and the Gamepass ID are both required for this method to work properly.

else MarketplaceService:PromptGamePassPurchase(Player, gamepass)

I also just noticed that this alternative condition is lined up with the incorrect conditional statement (which means that it would only be prompted if the player already owns the gamepass and does not have the item in their Backpack already:

In order to resolve this, the else statement should be moved outside of the end that closes off the “if not Find then” statement:

	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
		local Find = Player.Backpack:FindFirstChild("WaterBottle")
		if not Find then
			local h = game.ReplicatedStorage.WaterBottle:Clone()
			h.Parent = Player:WaitForChild("Backpack")
			print("Bottle Given.")
		end	

    else
        MarketplaceService:PromptGamePassPurchase(Player, gamepass)
	end
1 Like

What exactly isn’t working? @Black1fied

It wont give me the water bottle and there is nothing in the output.

Is it not prompting the gamepass, if so refer to what @StrongBigeMan9 just said with the parameters.

The part about giving me the water bottle isnt working.

Ok, can you please resend the current code what you have now.

1 Like
local Players = game:GetService("Players")
local part = script.Parent
local MarketplaceService = game:GetService("MarketplaceService")

part.Touched:Connect(function(hit)
	if not (Players:GetPlayerFromCharacter(hit.Parent)) then return end

	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	local gamepass = 26096507

	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
		local Find = Player.Backpack:FindFirstChild("WaterBottle")
		if not Find then
			local h = game.ReplicatedStorage.WaterBottle:Clone()
			h.Parent = Player:WaitForChild("Backpack")
			print("Bottle Given.")
		else MarketplaceService:PromptGamePassPurchase(Player, gamepass)
		end	
	end
end)

Ok! I think i’ve found the solution to your problem. In your code:

if not Find then
	local h = game.ReplicatedStorage.WaterBottle:Clone()
	h.Parent = Player:WaitForChild("Backpack")
	print("Bottle Given.")
	else MarketplaceService:PromptGamePassPurchase(Player, gamepass)
end		

You run the else statement if it doesn’t find the water bottle in the backpack, so a simple fix to that would be moving that else statement outside of that if statement so it would be:

	if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamepass) then
		local Find = Player.Backpack:FindFirstChild("WaterBottle")
		if not Find then
			local h = game.ReplicatedStorage.WaterBottle:Clone()
			h.Parent = Player:WaitForChild("Backpack")
			print("Bottle Given.")
		end	

    else
        MarketplaceService:PromptGamePassPurchase(Player, gamepass)
	end

Please mark this as correct if this helps :smile: !

2 Likes

@dantebyitself @StrongBigeMan9
Thank you guys for helping with my script. All solved!

1 Like