The ScrollingFrame contain a players list that is clickable and mute their boombox sound. At the moment, the script is taking the name of all the players in the server and adding it to the list
But I want it to be added to the list ONLY players names who own the gamepass.
Does anyone have any idea how to get all the players on the server and before cloning their name on the list, check if they have the gamepass
I was trying this:
but does not work
local list = script.Parent.BoomboxBackGround.PlayerList
local template = game.ReplicatedStorage.BoomboxMute.PlayerNameTemplate
local MPS = game:GetService("MarketplaceService")
while wait() do
game.Players.PlayerRemoving:Connect(function(plrLeft)
if list:FindFirstChild(plrLeft.Name) then list[plrLeft.Name]:Destroy() end
end)
for i, plr in pairs(game.Players:GetChildren()) do
if MPS:UserOwnsGamePassAsync(plr.UserId, 12345567) then
if not list:FindFirstChild(plr.Name) and plr ~= game.Players.LocalPlayer then
local plrName = template:Clone()
plrName.Text = plr.Name
plrName.Name = plr.Name
plrName.Parent = list
end
end
end
end
1. The boombox is purchased via a gamepass
If that is your case, then you can use the Marketplace to check if someone owns a specific gamepass, and if they do, then add them to the list. This should just be another if statement before adding a player to the list.
2. Check if the Player has the boombox equipped or in their backpack
Basically, you simply check if the player has a boombox somewhere. If the boombox is equipped, it should be inside their character. If it’s not equipped, it should be inside the backpack. This would require a bit more work, as you would have to loop through every item in the backpack, but it would still work.
I’m too lazy to write down this simple code, but it should not be a huge addition. You can do it!
local list = script.Parent.BoomboxBackGround.PlayerList
local template = game.ReplicatedStorage.BoomboxMute.PlayerNameTemplate
local MPS = game:GetService("MarketplaceService")
local player = game.Players.LocalPlayer
while wait() do
game.Players.PlayerRemoving:Connect(function(plrLeft)
if list:FindFirstChild(plrLeft.Name) then list[plrLeft.Name]:Destroy() end
end)
for i, plr in pairs(game.Players:GetChildren()) do
if not list:FindFirstChild(plr.Name) and plr ~= game.Players.LocalPlayer then
if MPS:UserOwnsGamePassAsync(player.UserId, 12345567) then
local plrName = template:Clone()
plrName.Text = plr.Name
plrName.Name = plr.Name
plrName.Parent = list
end
end
end
end
The script still has some flaws, for example the PlayerRemoving event is getting connected multiple times(every time a player leaves, it runs for a ridiculous amount of times). Also the direct UserOwnsGamePassAsync API request can fail, causing the entire script to break(by rising an error). I got rid of the unnecessary loop, added a pcall to handle the request, and switched to an event-based solution:
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
--you may want to use :WaitForChild here, if the list or template load after the script runs
local list = script.Parent.BoomboxBackGround.PlayerList
local template = ReplicatedStorage.BoomboxMute.PlayerNameTemplate
--decided to make it a pcall, so in case it fails it wont break the script.
function OwnsPass(userId, id)
local owned = false
local Success, Error = pcall(function()
owned = MarketplaceService:UserOwnsGamePassAsync(userId, id)
end)
if Error then
warn(Error)
end
return owned
end
function PlayerAdded(plr)
if not list:FindFirstChild(plr.Name) and plr ~= Player then
if OwnsPass(plr.UserId, 12345567) then
local frame = template:Clone()
frame.Text = plr.Name
frame.Name = plr.Name
frame.Parent = list
end
end
end
--Adding the in-game players, and listening for new players
for _, plr in pairs(Players:GetPlayers()) do
PlayerAdded(plr)
end
Players.PlayerAdded:Connect(PlayerAdded)
--only connecting the PlayerRemoving event once!
Players.PlayerRemoving:Connect(function(plr)
local frame = list:FindFirstChild(plr.Name)
if frame then
frame:Destroy()
end
end)