Gamepass giver is NOT working!

Hey!

I am trying to script a gamepass giver by default if you own a gamepass.

game.Players.PlayerAdded:connect(function(player)
    while true do
        if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 6583996) then
            if player.Backpack:FindFirstChild("GreenBalloon") == nil and player.Character:FindFirstChild("GreenBalloon") == nil then
                print(player.Name .. " got an item!")
                local b = game.ServerStorage:FindFirstChild("GreenBalloon"):Clone()
                b.Parent = player.Backpack
            end
        end
        wait(1)    
    end
end)

that is the current script we have, but it worked in the old verision and not in the new version.

image

its in SS, i tried checking output but that wasnt of use.

might want to replace while true do with

player.CharacterAdded:Connect(function()

end)

can you please rewrite the script?

Sometimes PlayerAdded can be finicky in studio. It sometimes doesn’t fire for the play solo player. This code should work in the game and in studio:

local function playerAdded(player)
    local function onChar(char)
        if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId, 6583996) then
            if player.Backpack:FindFirstChild("GreenBalloon") == nil and player.Character:FindFirstChild("GreenBalloon") == nil then
                print(player.Name .. " got an item!")
                local b = game.ServerStorage:FindFirstChild("GreenBalloon"):Clone()
                b.Parent = player.Backpack
            end
        end
	end
	
	onChar(player.Character or player.CharacterAdded:Wait())
    player.CharacterAdded:Connect(function(char)
        onChar(char)
    end)
end

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

for _, player in pairs(game.Players:GetPlayers()) do
	playerAdded(player)
end

This accounts for if players are already in the game by the time your script runs and for new players who join after it runs.

3 Likes

That’s not what this category is for. Please do not ask others to write scripts for you. See the Scripting Support category guidelines for more information on how to use this category.

The piece of advice shared was that you change your while loop to use the CharacterAdded event of players. Each time their character respawns and this signal is fired, it’ll run your code. There are also various examples of game pass tool scripts you can find via the toolbox that cover this.

The issue here is that you’re using a non-terminating loop to give a tool to a player, which is holding up the rest of the code. Whenever a player enters, a while loop spawns which is not what you want. Aiming to have an event-driven codebase produces the best results.

3 Likes