How would you script a gamepass to give you gear in-game

I am reasonably new to scripting and I am working on a game but I need help making a gamepass that in-game gives you a piece of gear.

Thanks in advanced

6 Likes

You would need to use a Markpetplace service to check if the user owns the asset Id.
For example if your storing the ‘gear’ in the ServerStorage it will go something like this…

game.Players.PlayerAdded:connect(function(player)
local mkps = game:GetService("MarketplaceService")
local ss = game.ServerStorage

repeat wait(0.1) until player.Backpack
repeat wait(0.1) until player.StarterGear

if mkps:UserOwnsGamePassAsync(player.UserId, GAMEPASSID) then
ss.Sword:Clone().Parent = player.Backpack
ss.Sword:Clone().Parent = player.StarterGear
end

end)

For future reference show what you have done already therefore we can help.

6 Likes

THANK YOU SO MUCH. I have seen millions (exaggeration) of youtube tutorials but all of them are outdated.

And this is myfirst scripting post so I didnt know.

Thanks again

3 Likes

No problem, if you ever need help again go to the Developer Hub

for example for this perticular topic you would go to the Developer Hub and look up gamepasses
and it would show this Passes | Documentation - Roblox Creator Hub

Then once your stuck from tutorials come to here.

2 Likes

Please use Instance:WaitForChild(Child Name) not repeat, this is just really inefficient
It takes 0 seconds if the object already exists

using repeat wait() until condition end is a really bad practise and should be shunned upon

2 Likes

What is the child name?
I need thirty characters

1 Like

An example of WaitForChild:

local PlayerGui = Player:WaitForChild("PlayerGui") --child name is the name of the instance you wish to wait for
1 Like

It doesn’t seem to work this is my code. I may have gone wrong.

game.Players.PlayerAdded:connect(function(player)

local mkps = game:GetService(“MarketplaceService”)

local ss = game.ServerStorage

local PlayerGui = Player:WaitForChild(“PlayerGui”) --child name is the name of the instance you wish to wait for

if mkps:UserOwnsGamePassAsync(player.UserId, 9141220) then

ss.Sniper3:Clone().Parent = player.Backpack

ss.Sniper3:Clone().Parent = player.StarterGear

end

end)

1 Like

The Player is defined as player in your PlayerAdded event. You are referring to the Player as Player in the :WaitForChild() line, giving you an error as Player is returning nil. Pay attention to capitalization! :slight_smile:

Fixed code:

local PlayerGui = player:WaitForChild(“PlayerGui”)
3 Likes