Is this script supposed to work

im trying to make it so when u buy a gamepass u get an rpg i made the purhcase button but the rpg giving when u have the pass doesnt work heres the script:

local Players = game:GetService("Players")
local MarketPlaceService =  game:GetService("MarketplaceService")
local GamepassID = 940711679 -- Change this ID to your Gamepass ID
local ToolName = "RPG" -- Change YourToolName to whatever you tool is called

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
			script[ToolName]:Clone().Parent = player.Backpack
			print("Given")
		end
	end)
end) 

Does it print out “Given”? Also there shouldn’t be a need to have it placed in CharacterAdded (sometimes characteradded wont work in studio on your first load through since its too fast).

Try this revision which just places the item in their startergear and their backpack.

Players.PlayerAdded:Connect(function(player)
if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
			script[ToolName]:Clone().Parent = player.Backpack
script[ToolName]:Clone().Parent = player.Startergear
			print("Given")
		end
end)

i dont think it should be player added now that im thinking

ur version also ddint work, so what do i do

Are you seeing the console print “Given” like @StraightScared mentioned?

In my experience, this is true for PlayerAdded too. It probably worth moving the code to it’s own function then looping over all the existing players after the connection is set.

local function HandlePlayerAdded(Player)
    if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
        script[ToolName]:Clone().Parent = player.Backpack
        print("Given")
    end
end

Players.PlayerAdded:Connect(HandlePlayerAdded)

-- Run for already existing players
for _, Player in Players:GetChildren() do
    HandlePlayerAdded(Player)
end

it doesnt print im gonna try this one rn

doesnt give and no print of given happens idk whats the problem

will it only work in roblox or what