Why won't this radio spawn in my inventory?

local ASSET_ID = 11261938
 
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local PlayerOwnsAsset = MarketplaceService.PlayerOwnsAsset
 
Players.PlayerAdded:Connect(function (plr)
	local success, doesPlayerOwnAsset = pcall(PlayerOwnsAsset, MarketplaceService, plr, ASSET_ID)
	if doesPlayerOwnAsset then
		local radio = game.ServerStorage.BoomBox:Clone()
		radio.Parent = plr.Backpack
	else
		print(plr.Name .. " doesn't own " .. ASSET_ID)
	end
end)

This script is supposed to give the people who own the radio gamepass a radio, why doesn’t it work? I found it from the developer hub on roblox when researching. Did I do something wrong with it? This is a script in serverscriptservice.

1 Like

Do you see any errors in your output window?

1 Like

Nope. It just prints that I don’t own the asset, am I supposed to buy my own asset? Also I’ve tried buying it on an alt, and still nothing happened.

Is the asset you are trying to sell a gamepass?
Edit: never mind, I re-read your post

1 Like

Yes it is. - It is a radio that plays music when you type the ID into it.

Since it’s a gamepass, I would use :UserOwnsGamePassAsync()

An example would be:

local MarketplaceService = game:GetService("MarketplaceService")
local gamepassId = -- your gamepass Id

game.Players.PlayerAdded:Connect(function(plr)
    if MarketplaceService:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
        print("User owns the gamepass")
        -- rest of your code
    else
        print("User does not own the gamepass")
    end
end)
2 Likes

This still doesn’t work, it doesn’t place the boombox in my inventory.

Did you replace the comment with your code that creates the radio?

1 Like

Yes.

	
	local mkp = game:GetService("MarketplaceService")
	if mkp:UserOwnsGamePassAsync(p.UserId, 11261938) then
		
		local boombox = game.ServerStorage.BoomBox:Clone()
		boombox.Parent = p.Backpack
	else
		return
	end
end)

I fully agree with @P1X3L_K1NG with regards to using UserOwnsGamePassAsync for your use case.

The issue now is that the code needs to run every time the player respawns as the Backpack resets on respawn. I also think the PlayerAdded event fires before the Character spawns, so this is why it might not even work when the player spawns for the first time.

A CharacterAdded event is needed so that boombox is added to the inventory every time the player respawns:

local MarketplaceService = game:GetService("MarketplaceService")
local GAMEPASS_ID = 0 -- GamePass ID

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character) -- This will now run every time the character is added
		if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAMEPASS_ID) then
			-- User owns the gamepass!
			local boombox = game.ServerStorage.BoomBox:Clone()
			boombox.Parent = player.Backpack
		end
	end)
end)
3 Likes

Thank you! I’ll try this, stay tuned.

1 Like