Gamepass giver is not working

Hey, so I am currently developing a vibe game and I made a script that’s meant to give you a glowstick if you own the gamepass, it works in studio but in an actual roblox server it gives random items, could someone help?

Script:

local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)

local gamePassID = 17721832

function Spawned(player)

local HasGamepass = false

local success, message = pcall(function()
 	HasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
end)

if not success then
	 warn("Checking In Player Has Gamepass" .. tostring(message))
   return
end

if HasGamepass == true then -- If they own the gamepass
	game.ServerStorage.GlowStick:Clone().Parent = player.Backpack 
	end 
end

game.Players.PlayerAdded:Connect(function(player) -- Player added to game
	player.CharacterAdded:Connect(function()
	Spawned(player)
end)

end)

Players.PlayerSpawned:Connect(Spawned)

glow1

Move this topic to #help-and-feedback:scripting-support

3 Likes

change this line

Players.PlayerSpawned:Connect(Spawned)

to this

Players.PlayerAdded:Connect(Spawned)

and it should work

local MPS = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local GamepassID = 17721832

local function Spawned(Player)
    local HasGamepass = false

    Player.CharacterAdded:Connect(function(Character)
        local success, message = pcall(function()
            HasGamepass = MPS:UserOwnsGamePassAsync(Player.UserId, GamepassID)
        end)

        if not success then
            warn("Checking in Player has gamepass: ", message) --No need to return, there's nothing else in the script that you need to return back early
        end

        if HasGamepass == true then --If they own the gamepass
            game.ServerStorage.GlowStick:Clone().Parent = Player.Backpack
        end

    end)
end

Players.PlayerAdded:Connect(Spawned)

This should work?

it works in studio but then in game its still spawning the random items

What do you mean by spawning random items? There’s nothing in this script that could be cloning items in random positions?

I know that’s what’s confusing me so much in studio it works fine but in game it spawns all these stupid things

Are there any other scripts that could be spawning those random items?

checked every single one and couldnt find any spawn scripts inside anything i just disabled the backpack i dont wanna mess around with it anymore but thanks for trying to help

Does the item you are trying to spawn have a script inside it that rotates through various random items ? Or does one of the other items in your game have a script that is causing this to happen ?

the script is in the post scroll up

Can you try this…

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

local gamePassID = 17721832

game.Players.PlayerAdded:Connect(function(player)
	
	
	player.CharacterAdded:Connect(function()
		
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	if hasPass then
		print("player has the gamepass")

		local GlowStick = game.ReplicatedStorage.GlowStick:Clone()
		GlowStick.Parent = player.Backpack

		end
	end)
		
	
	
end)




local function onPromptGamePassPurchaseFinished(player, purchasedPassID, purchaseSuccess)

	if purchaseSuccess == true and purchasedPassID == gamePassID then
		print(player.Name.."purchased the game pass!")

		local GlowStick = game.ReplicatedStorage.GlowStick:Clone()
		GlowStick.Parent = player.Backpack
	end
		
end





MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptGamePassPurchaseFinished)

Also make sure that script is in Server Script Service and the item that you want the gamepass to give is in Replicated Storage. Be sure your item name is spelled correctly and matches in the script. You should not have more than one item with the same name.

Do you have a Shop Gui setup where they purchase the gamepass ?
If not I can give you detailed instructions for that as well.

In the last image, are they objects in your game? If not, look at this post, the same thing had happened to me. With the rest, the code looks good.

This might work:

local mps = game:GetService("MarketplaceService") 

local function checkplrgamepass(plr)
    if mps:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
         game.ReplicatedStorage:WaitForChild("GlowStick"):Clone().Parent = player.Backpack -- Placing the tool in replicated storage would be better
           print("Player has recieved the gamepass tool")
      else
           print("Player don't have gamepass for tool") -- tells player don't have gamepass
    end
end)

game.Players.PlayerAdded:Connect(function(player)
     wait(5) -- cooldown for loading if anything to load in the server
     checkplrgamepass(player)
end)

Basically, when the first player joins the server might not loading fully so it can say “tool not found”, so this script might help it work.

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

local gamePassID = 17721832

function Spawned(player)
local HasGamepass = false

local success, message = pcall(function()
 	HasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)

if not success then
   return warn("Checking In Player Has Gamepass" .. tostring(message))
end

if not HasGamepass then 
return warn('Player does not own Gamepass.') 
end

player.CharacterAdded:Connect(function()
game.ServerStorage.GlowStick:Clone().Parent = player.Backpack
end)
end

game.Players.PlayerAdded:Connect(Spawned)

Does the game have gears enabled? a lot of the time, this is the issue.

In studio it was spawning the gamepass items like I wanted but then in an actual server it would give random stuff.

Thank you so much! You don’t know how long I spent looking through scripts and parts.