How to give purchased tool to player?

Alright folks, I’ve already posted about this, but the solution didn’t work for me. I’m genuinely confused about how everyone is doing this. I’m not a big scripter guy or one to stick around during tutorials BUT I’ve looked everywhere and have no idea how to do this! I have a gamepass, and I want to make it so that when people buy it, they are given a tool. Please help, I’m confused.

2 Likes

Like this?

local passId = 0
local MarketplaceService = game:GetService('MarketplaceService')
local toolLoc = game:GetService('ServerStorage'):WaitForChild('Tool') -- your tool location & name here

game.Players.PlayerAdded:Connect(function(player)
   if MarketplaceService:UserOwnsGamePassAsync(player.UserId, passId) then
      local backpack = player:WaitForChild('Backpack')
      toolLoc:Clone().Parent = backpack
   end
end)

This grants the player the tool if they own that gamepass when they join the game.

5 Likes

Do I place this as a script in the workplace?
Also, it says there is a syntax error.

1 Like

You need to declare ServerStorage. The script can go anywhere it will run (workspace or serverscriptservice).

#help-and-feedback:scripting-support is for learning programming, programming questions, and help fixing code. You can ask for entire scripts in #collaboration:recruitment

Edit:
Here’s an example of how to declare a variable.
local ServerStorage = game:GetService('ServerStorage')
Edit:
Original script edited :+1:

2 Likes

This is meant to be a server script in ServerScriptService, as the client cannot access ServerStorage.

Note workspace scripts are server sided :+1:

So what is it? Do I put it in the SSS or Workspace?

It can be placed in either place, but I prefer SSS for organization purposes.

Anyways, I needed this script for a group game. Unfortunately, because you don’t own the gamepasses you make in group games, I moved to a different game. In this game, there was a gamepass that was supposed to give you a sign. I tried a different script and it didn’t work. Now, I am unable to get this script to work. Here’s a screenshot of the adapted script you handed me.


Also, here is the original gamepass if that helps us collectively figure this out. Support Sign - Roblox

You forgot to close the PlayerAdded function with an end)

It still has a syntax error if I close it with a parentheses.

game.Players.PlayerAdded:Connect(function(player)
   if MarketplaceService:UserOwnsGamePassAsync(player.UserId, passId) then
      local backpack = player:WaitForChild('Backpack')
      toolLoc:Clone().Parent = backpack
   end
end)

Can you show me the error? @mebigrouxboy

Isn’t it the user id, not the Player instance? Try changing player to player.UserId

It disappeared before my very eyes…
magickk
We should move this conversation to messages.

Make a script in ServerScriptService, and type this:

local marketplaceService = game:GetService("MarketplaceService")
local id = 0 -- place your gamepass id here
local tool = game:GetService("ServerStorage"):WaitForChild("Tool") -- path to tool

marketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, idBuying, purchased)
    if purchased and idBuying == id then
        tool:Clone().Parent = player:WaitForChild("Backpack")
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    if marketplaceService:UserOwnsGamePassAsync(player.UserId, id) then
        tool:Clone().Parent = player:WaitForChild("Backpack")
    end
end)
1 Like

So I’ve been reading through the comments, and rather than giving you the script, I’d much rather explain how you’d do it…

So the main thing we need to figure out is if the Player owns a certain gamepass, and by far the easiest way to do this is by using MarketplaceService. There is a function under that service called “UserOwnsGamePassAsync()” which will take the UserId of the player and the gamepass ID and return true or false depending if it is owned…

So we got that part settled, now how do we give a tool? You do that by parenting it to the Backpack and/or StarterGear. If we parent it to the Backpack, the player will only receive it once, and if we parent it to StarterGear, they will receive after every life. To clone you can simply use :Clone() on the tool then parent it to whichever one you’d like.

And that’s pretty much it honestly… Lets put it together

Lets start off by declaring a few variables to make it easier

local MarketplaceService = game:GetService("MarketplaceService")
local Tool = game:GetService("ServerStorage"):WaitForChild("ToolName") -- You can change this line however you need it, it just needs to lead to the tool. 

local PassID = 0000000 -- Replace with actual ID of course 

So now we got some basic variables declared, since we want them to receive the tool whenever they join the game, lets start off by using the PlayerAdded event

local MarketplaceService = game:GetService("MarketplaceService")
local Tool = game:GetService("ServerStorage"):WaitForChild("ToolName") -- You can change this line however you need it, it just needs to lead to the tool. 

local PassID = 0000000 -- Replace with actual ID of course 

function PlayerAdded(Player)

end

game:GetService("Players").PlayerAdded:Connect(PlayerAdded)

So now we just have to check to see if they own the pass using the method described above.


local MarketplaceService = game:GetService("MarketplaceService")
local Tool = game:GetService("ServerStorage"):WaitForChild("ToolName") -- You can change this line however you need it, it just needs to lead to the tool. 

local PassID = 0000000 -- Replace with actual ID of course 

function PlayerAdded(Player)
     if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, PassID) then

     end
end

game:GetService("Players").PlayerAdded:Connect(PlayerAdded)

And finally we need to clone the tool, in this case, Im going to just clone it into the Backpack, but feel free to do it however you’d like.


local MarketplaceService = game:GetService("MarketplaceService")
local Tool = game:GetService("ServerStorage"):WaitForChild("ToolName") -- You can change this line however you need it, it just needs to lead to the tool. 

local PassID = 0000000 -- Replace with actual ID of course 

function PlayerAdded(Player)
     if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, PassID) then
          local ClonedTool = Tool:Clone()
          ClonedTool.Parent = Player.Backpack
     end
end

game:GetService("Players").PlayerAdded:Connect(PlayerAdded)

And we’re done… I highly suggest you don’t copy and paste the code, it is much easier to learn it if you’re typing it out… If you have any questions, be sure to let me know!

2 Likes