Boombox Function Wouldn't Work

I was recently creating a gamepass for the player to play their own music in my game, and the gamepass was inserted fine. The only problem is that the gamepass wouldn’t actually work.

The tool itself is fine. It works when I place it in my player’s StarterPack, but it doesn’t work when I place it in ReplicatedStorage to be cloned as a gamepass item.

The gamepass item is a boombox. The gear is inserted into the player’s inventory, and they can hold it out. But when you click, the Music ID input wouldn’t appear (Where it asks for an audio ID, and has a play button beneath it)
Example photo: image

I didn’t use a model from the Toolbox, I inserted the gear ID into the EasyInsert plugin (EasyInsert is a plugin that allows you to insert items through its ID. This makes it helpful to insert player models, assets, and functioning gears)

Gear ID Origin Item

Golden Super Fly Boombox - Roblox

This is the current script:

This script is currently placed in StarterGUI as a local script. The boombox tool is placed in ReplicatedStorage.

TL:DR: Boombox item is inserted to backpack when the user owns the gamepass, but the boombox doesn’t work in-game.

Solution:

local players = game:GetService("Players") -- the players service
local market = game:GetService("MarketplaceService") -- marketplace service

local function playerOwnsGamepass(player, id)
   -- function used to check if the player owns a game pass
   return market:UserOwnsGamePassAsync(player.UserId, id) 
   -- return either true/false
   -- if roblox's servers are down, this can error. To combat that, you'd have to wrap this in a pcall
end

local function onPlayerAdded(player)
   if playerOwnsGamepass(player, 17831009) then
      -- since the function returns a boolean (true/false) you can structure it like this since it's only used once
      -- copies the boombox into the player's inventory
      game:GetService("ReplicatedStorage"):WaitForChild("SuperFlyGoldBoombox"):Clone().Parent = player.Backpack
   end
end

players.PlayerAdded:Connect(onPlayerAdded)
-- function/event to detect when a player joins and calls the "onPlayerAdded" function

for _, plr in ipairs(players:GetPlayers()) do
   -- loops through possible players who're in the game/connected before this script loaded
   coroutine.wrap(onPlayerAdded)(plr) -- wrapped in a coroutine so that the loop can progress without waiting
end
1 Like

This is unrelated to your problem but if you’re doing this on the client then exploiters can easily bypass this and give themselves the item.

Also, your issue may have to do with the fact that you’re cloning it from the client. The boombox has a server script, meaning, when the local script clones it, the server script won’t run because on the server, the server script is still in replicated storage.

Well, first of all, did you update the gamepass ID to your own ID in the script?

I did. The gamepass ID on the script is correct.

The gear is inserted into the inventory just fine. It’s the gear’s function that is the problem.

Hmm, then you should take a look at HugeCoolboy’s reply, because everything in there is very true.

Perhaps I should record the problem for a better understanding.

And it would also help if you posted what the output looks like.

HugeCoolboy is right. I cloned the boombox from the server and it worked perfectly.
Then I tried cloning it from the local script and the gui didn’t show up.

So should I try to clone it from a Script instead of a LocalScript?

Yes.
You should check the gamepass and clone it in a Script.

Yes, because if you clone it from a local script, the server would not know that it was even cloned in the first place.

Alright, let me test it.

If it works as intended, I’ll mark your post as a solution.

In-case you don’t know how to structure your server script code, it should be like this:

local players = game:GetService("Players") -- the players service
local market = game:GetService("MarketplaceService") -- marketplace service

local function playerOwnsGamepass(player, id)
   -- function used to check if the player owns a game pass
   return market:UserOwnsGamePassAsync(player.UserId, id) 
   -- return either true/false
   -- if roblox's servers are down, this can error. To combat that, you'd have to wrap this in a pcall
end

local function onPlayerAdded(player)
   if playerOwnsGamepass(player, 17831009) then
      -- since the function returns a boolean (true/false) you can structure it like this since it's only used once
      -- copies the boombox into the player's inventory
      game:GetService("ReplicatedStorage"):WaitForChild("SuperFlyGoldBoombox"):Clone().Parent = player.Backpack
   end
end

players.PlayerAdded:Connect(onPlayerAdded)
-- function/event to detect when a player joins and calls the "onPlayerAdded" function

for _, plr in ipairs(players:GetPlayers()) do
   -- loops through possible players who're in the game/connected before this script loaded
   coroutine.wrap(onPlayerAdded)(plr) -- wrapped in a coroutine so that the loop can progress without waiting
end

It should be in ServerScriptService.

I recommend reading this: Client-Server Model | Roblox Creator Documentation

4 Likes

After testing the code, it seems that the boombox wouldn’t appear in the player’s inventory. I don’t know if its on my part, but I did follow what you instructed on the code.

The server script was placed into ServerScriptService

image

The tool is still in ReplicatedStorage
image

But there is no boombox clone.
image

The only change I made was deleting the LocalScript originally.

Are there any changes I need to make on the provided script?

1 Like

I might’ve made a slight typo somewhere (I was in a slight rush). You can try to see if the output has any errors. If it doesn’t, you can create print calls for debugging and to see what the script is doing.

example

local function onPlayerAdded(player)
   if playerOwnsGamepass(player, 17831009) then
      print(player.Name, "owns the gamepass")
      game:GetService("ReplicatedStorage"):WaitForChild("SuperFlyGoldBoombox"):Clone().Parent = player.Backpack
   else
      print(player.Name, "does not own the gamepass")
   end
end

The output log should print one of these and that could help you a bit.

1 Like

It seems that Roblox Studio is labeling the “UserOwnsGamepassAsync” function is not a member of MarketplaceService.

Gonna do some debugging.

Found that the GamepassAsync has a typo, and should be UserOwnsGamePassAsync

2 Likes

Alright, the item is working great now. Thanks!

2 Likes