Showing an image button for the player who owns the gamepass

Hello,

So I already know how I can give abilities to players with stuff like swords, but now I want to know how
you can make a image button visible for only the player who owns the gamepass.

The issue is that when I try to make it it shows to all other players in the server.

Here’s the code;

local MarketplaceService = game:GetService(“MarketplaceService”)
local Players = game:GetService(“Players”)

local gamePassID = ********

local function onPlayerAdded(player)

local hasPass = false


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

-- If there's an error, issue a warning and exit the function
if not success then
	warn("Error while checking if player has pass: " .. tostring(message))
	return
end

if hasPass == true then
	print(player.Name .. " owns the game pass with ID " .. gamePassID)
	game.StarterGui.Menu.Menu2Frame.Selection.SelectionFrame.ImageButton.Visible = true
end

end

–Connect “PlayerAdded” events to the “onPlayerAdded()” function
Players.PlayerAdded:Connect(onPlayerAdded)

1 Like

Instead of using game.StarterGui, use player.PlayerGui. Otherwise, the GUI would be visible for everyone.

I tried that but the image button is now visible to no one.

Wow, im such dumb i had to put the id oof.

Check the output. Does this line work?
print(player.Name .. " owns the game pass with ID " .. gamePassID)

That works but there is an error code;

'Menu is not a valid member of PlayerGui “Players.henrydanger5472.PlayerGui”

Try adding a wait(1) right before the gui line. It could be because the player’s uis havent loaded yet.

That doesnt works either. And my ui’s are loading very quick tho.

Image buttons might take a while to load, since downloading the image from the server might take a while. Use :WaitForChild when referencing it, does that work?

Does :WaitForChild needs to go after player?

Judging from the output error, it’s because you don’t have the Menu gui on your PlayerGui. Make sure that it is the case by going on test mode and looking on your PlayerGui. It is also important to know the the names are caps sensitive.

image

Should I clone it into PlayerGui then?

Change that to:

local player = game.Players.LocalPlayer
game.Players[player].PlayerGui.Menu.Selection.SelectionFrame:WaitForChild("ImageButton") = true

Anything that is in StarterGui automaticly gets cloned to PlayerGui. If you change anything on the StarterGui, it changes for everyone else. Basically what I’m saying is, you don’t even have the Menu gui on StarterGui which is why there is no Menu gui on the PlayerGui.

image
its telling that error

Try changing that with

player.PlayerGui.Menu.Selection.SelectionFrame:WaitForChild("ImageButton").Visible = true
2 Likes

Sorry for not replying but my studio got crashed but im testing it now @flkfv.

Thank you all for your help! :upside_down_face: