Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need to make gui visible on prompt but it will not work?

I have a gamepass that when you get it I need it to make a few buttons visible

local player = game.Players.LocalPlayer
local ownsGamepass = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId,97155762) ------ Your Gamepass ID here

if ownsGamepass then
	local soccerworldbutton = game.StarterGui.ScreenGui.MainFrame.soccerworld
	local cityworldbutton = game.StarterGui.ScreenGui.MainFrame.gotocity
	soccerworldbutton.Visible = true
	cityworldbutton.Visible = true
end

I ran a print and everything works but the gui turning visible. please help

1 answer

✓ Accepted answer

Whenever you are trying to access a ScreenGui from the client you do not use StarterGui. StarterGui is just the container of all of the ScreenGui's that a play would receive upon joining your game. Instead you would want to access the LocalPlayer's PlayerGui.

Whenever you are doing yielding calls, such as, UserOwnsGamePassAsync() make sure to use pcalls like I did below to prevent your entire codebase from erroring, and you not knowing why.

local MarketplaceService = game:GetService("MarketplaceService")

local GAMEPASS_ID = 97155762

local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

local mainGui = playerGui:WaitForChild("ScreenGui")
local mainFrame = mainGui:WaitForChild("MainFrame")
local soccerWorldButton = mainFrame:WaitForChild("soccerworld")
local cityWorldButton = mainFrame:WaitForChild("gotocity")

local ownsGamepass = false

local success, err = pcall(function()
	ownsGamepass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, GAMEPASS_ID)
end)

-- Something errored when this was called, so it will print the error
if not success then
	warn(tostring(err)
	return
end

-- Now we can simply remove that if statement, and do this instead to simplify your code
soccerWorldButton.Visible = ownsGamepass
cityWorldButton = ownsGamepass

This discussion is preserved as it was. New answers, comments, and votes are closed.