I'm trying to make a gamepass script

I am trying to make a script that gives a piece of gear to the buyer but it’s not working.

local id = 10977515

game.Players.PlayerAdded:connect(function(player)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then
		game.ReplicatedStorage.M82:Clone().Parent = player:WaitForChild("StarterGear") 
	end
end)

Here is the script, can you guys see anything wrong with it?

1 Like

I’m not a amazing programmer, I don’t really know whats wrong with it at the moment. But I’m on mobile and I found this script in one of my documents. It seems to work.


local MarketPlaceService = game:GetService(“MarketplaceService”)
local GamepassID = NUMBER – GAMEPASSID

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

if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then

game.ServerStorage.ITEMNAME:Clone().Parent = player:WaitForChild(“Backpack”)
game.ServerStorage.ITEMNAME:Clone().Parent = player:WaitForChild(“StarterGear”)
end
end)

1 Like

Do I put the gear in serverstorage

Yeah, I’m pretty sure.

NOTE: If this script doesn’t work, I have two more.

I still only get the gear I have already from the team.

Did you fix the quotes in the code? Those seem to have changed when pasted.

Well, I have no idea at this moment in time.

Let’s let the pros help.

are you sure that the player’s id go first on the parameters?

If the script given by Adam doesn’t work, you can try to follow this tutorial which I found with a google search.

1 Like

Sometimes the character can load before the UserOwnsGamePass is able to return the result, a way to fix this could be to see if the character already exists and if it does, clone it to the backpack as well.

local id = 10977515
local MarketplaceService = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(plr)
	if (MarketplaceService:UserOwnsGamePassAsync(plr.UserId,id)) then
		game.ReplicatedStorage.M82:Clone().Parent = plr:WaitForChild("StarterGear")
		if (plr.Character) then
			game.ReplicatedStorage.M82:Clone().Parent = plr:WaitForChild("Backpack")
		end
	end
end)
2 Likes

If that doesn’t work, here is a gamepass tool giver script I use in my games, and has worked like a charm since the past few months.

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

--[[

FORMAT:
{GamepassID,ToolLocation}

]]

local Gamepasses = {
	{gamepassid,ToolsReference};
}
local n = #Gamepasses

local c;c = game.Players.PlayerAdded:Connect(function(plr)
	for i = 1,n do
		local v = Gamepasses[i]
		local hasPass
		
		if (v[3]) then
			hasPass = v[3](plr)
		end
		
		if (not hasPass) then
			local s,e = pcall(function()
				hasPass = MarketplaceService:UserOwnsGamePassAsync(plr.UserId,v[1])
			end)
			if (not s) then
				warn("Error checking for gamepass '"..v[1].."' for player '"..plr.Name.."': "..e)
			end
		end
		
		if (hasPass) then
			v[2]:Clone().Parent = plr:WaitForChild("StarterGear")
			if (plr.Character) then
				v[2]:Clone().Parent = plr:WaitForChild("Backpack")
			end
		end
	end
end)

Im very new to scripting, so I’m guessing Gamepassid means the gamepass id and toolrefrence means the tools name?

Here is an example:

{123456,game.ServerStorage.Bow}

Your script is pretty similar to mine. :face_with_raised_eyebrow:

Good scripting though.

(BTW: the script that I showed was NOT the one I use in my games)

2 Likes

We’ll need to do some debugging. Put a print statement after every line, and wherever it doesn’t print is where the issue resides. It could be with the MPS line or the tool giving line, but do this just to make sure.

local id = 10977515

game.Players.PlayerAdded:connect(function(player)
    print("1")
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, id) then
        print("2")
		game.ReplicatedStorage.M82:Clone().Parent = player:WaitForChild("StarterGear") 
	end
end)
1 Like

Like this?

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

--[[

FORMAT:
{GamepassID,ToolLocation}

]]

local Gamepasses = {
	{10977515,game.ServerStorage.M82};
}
local n = #Gamepasses

local c;c = game.Players.PlayerAdded:Connect(function(plr)
	for i = 1,n do
		local v = Gamepasses[i]
		local hasPass
		
		if (v[3]) then
			hasPass = v[3](plr)
		end
		
		if (not hasPass) then
			local s,e = pcall(function()
				hasPass = MarketplaceService:UserOwnsGamePassAsync(plr.UserId,v[1])
			end)
			if (not s) then
				warn("Error checking for gamepass '"..v[1].."' for player '"..plr.Name.."': "..e)
			end
		end
		
		if (hasPass) then
			v[2]:Clone().Parent = plr:WaitForChild("StarterGear")
			if (plr.Character) then
				v[2]:Clone().Parent = plr:WaitForChild("Backpack")
			end
		end
	end
end)
1 Like

Is the tool’s Archivable property true?

1 Like

Yes it is true. Should it be false?

No. It should be true. According to the API reference, “Calling Clone directly on an object will return nil if the cloned object is not archivable.”

A post was merged into an existing topic: Feedback - Bypassing the 30 Character Limit