Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

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
Ad

Answer this question