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.
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)
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)