Script works in studio, but not in game

Howdy. I’m trying to create a radio gamepass script, and it works in studio, but not in game.

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

local Radio = ReplicatedStorage.BoomBox

local gamePassID = 15217035

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

	if success then
		print("Owns Radio!")
		
		local RadioClone = Radio:Clone() 
		RadioClone.Parent = player:WaitForChild("Backpack", math.huge)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)

This script is taken from the dev wiki with a few minor changes. I copied it to save time. The part thats not working is the part where it clones and gives the radio to the player. it gives the radio in studio, but not in-game. And yes, it does print “Owns radio!” in studio and in game. It just won’t give the radio.

1 Like

First of all, if you die, the radio will disappear no matter what. I suggest adding it to your player.StarterGear folder or using a CharacterAdded function instead. Also, UserOwnsGamePassAsync does not error if the player doesn’t own the gamepass, it will just return false, so you would be basically giving the radio to anyone who joins the game.

1 Like

Try putting the print(“Owns Radio”) comment after the RadioClone.Parent line, since that line has a WaitForChild in it that may not be coming true. Moving the print would let you know if that line is being run.

1 Like

Alright. This should fix it right?

local function onPlayerAdded(player)
	local hasGamepass
	local success, err = pcall(function()
		hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	player.CharacterAdded:Connect(function()
		if hasGamepass and success and not err then
			print("Owns Radio!")

			local RadioClone = Radio:Clone() 
			RadioClone.Parent = player:WaitForChild("Backpack", math.huge)
		end
	end)
end

I created a local variable called hasGamepass, which will hold the result that UserOwnsGamepassAsync() returns. Then, I check if it’s value is true(along with a few other conditions)

1 Like

UPDATE:

Everything is fixed.

local MarketplaceService = game:GetService("MarketplaceService")

local ServerStorage = game:GetService('ServerStorage')
local Radio = ServerStorage.BoomBox

local gamePassID = 15217035

local function onPlayerAdded(player)
	local hasGamepass
	
	local success, err = pcall(function()
		hasGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	if not success then
		warn("Error getting gamepass Async. Error: " .. err)
		return
	end


	if hasGamepass == true then
		player.CharacterAdded:Connect(function()
			print("Owns Radio!")

			local RadioClone = Radio:Clone() 
			RadioClone.Parent = player:WaitForChild("Backpack", math.huge)
		end)
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)