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

How do I access items within PlayerGui?

In the process of trying to make a Gui shop, I've just been using .Parent to access all of the players Guis and then descending into the child that I need to access.

local plyrGUI = script.Parent.Parent.Parent.Parent
local shopGUI = plyrGUI.ShopGui

I then tried to access it using services but it ends up saying "attempt to index nil with "ShopGui" " even though I know ShopGui is properly replicated into the player's PlayerGui.

local playerGUI = game.Players:GetChildren()

local function onButtonActivated()
	playerGUI.PlayerGui.ShopGui.Enabled = false
end

I want to put more ease into this method by just directly accessing a single player's PlayerGui (my game will be max player of one, so it doesn't need to include multiple players).

1 answer

✓ Accepted answer

It all depends on what you are trying to do.

Player = game.Players.LocalPlayer
plyrGUI = player.PlayerGui

What you do inside of GUIs is change a certain players way they see the GUI, so you’ll want to use a local script for the most part. All you need to put is what is written above if you are trying to get the playerGui when the script can be a local script.

script.Parent.ClickDetector.MouseClick:Connect(function(player))
   plyrGUI = player.PlayerGui
end)

With this way, when someone clicks a clickdetector, the parameter is the player that clicked it. With this, you can go directly to the PlayerGui just by knowing who clicked the button.

script.Parent.Touched:Connect(function(hit))
   if hit.Parent:FindFirstChild(“Humanoid”) then
       Player = game.Players:GetPlayerFromCharacter(hit.Parent)
       plyrGUI = Player.PlayerGui
  end
end)

This way uses the touched function to get the player from their character. The hit is the part of the humanoid that touched it. It then goes to the parent; the character. Then it goes from their to get the player from character, and lastly gets the PlayerGui from the player.

Lastly, you could always use a local script in a GUI, but if you wanted to make someone server side, you could fire and event.

I hope this helped!

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