Access PlayerGui from workspace

Does anyone know how to access PlayerGui from a script in workspace?

1 Like

You could Use a RemoteEvent :slight_smile:

To access the PlayerGui object from a script in the Workspace , you can use the Player.PlayerGui property. This property returns the PlayerGui object for the player who is running the script.

Here’s an example of how you can use this property:

local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui

-- Print the name of the player's PlayerGui
print(playerGui.Name)

If you want to access the PlayerGui object for a specific player, you can use the Players service and specify the player’s name or user ID. Here’s an example:

local player = game.Players:GetPlayerByName("Player1")
local playerGui = player.PlayerGui

-- Print the name of the player's PlayerGui
print(playerGui.Name)

In this example, the PlayerGui object for the player named “Player1” will be accessed and the name of the PlayerGui will be printed.

Note: The PlayerGui object is only available on the client side, so this code should be run on the client, not the server.

2 Likes

@Arylist You need to remember that LocalScripts Do not work inside the workspace

You are correct, my bad :skull: . LocalScript objects do not work inside the Workspace . LocalScript objects are only executed on the client and are used to run scripts that can access the player’s client-side objects, such as the PlayerGui object.

If you want to access the PlayerGui object from a script in the Workspace , you can use a Script object instead of a LocalScript object. Script objects can be executed on either the client or the server, and they can access the Workspace and other server-side objects.

Here’s an example of how you can use a Script object to access the PlayerGui object:

-- This script will run on the server and access the PlayerGui of the player who is running it
local player = game.Players.LocalPlayer
local playerGui = player.PlayerGui

-- Print the name of the player's PlayerGui
print(playerGui.Name)

Note: In order for this code to work, the Script object must be placed inside the Workspace and the player who is running it must have the Workspace object in their view.

1 Like

Here is the issue

leaveGuiEvent.OnServerEvent:Connect(function(player) -- Player
	if player.Character then
		local playerGui = player.PlayerGui
		player.Character.HumanoidRootPart.Anchored = false
		wait()
		player.Character.Humanoid.Jump = true
		wait()
		player.Character:MoveTo(game.Workspace.leaveRoomPart.Position)
		removeFromList(player.Character)
		playerGui.Telecam.Enabled = false
		playerGui.Telecamout.Enabled = true -- Works up to here
		playerGui.ScreenGui.Players.TextTransparency = "1" -- Here and on not working
		playerGui.ScreenGui.Time.TextTransparency = "1"
		playerGui.ScreenGui.Tele.TextTransparency = "1"
		wait(2.5)
		playerGui.ScreenGui.Title.Visible = true
		playerGui.ScreenGui.Inventory.Visible = true
		playerGui.ScreenGui.Play.Visible = true
		playerGui.ScreenGui.Shop.Visible = true
		playerGui.Telecamout.Enabled = false
		playerGui.Cutscene.Enabled = true
	end 
end)

what does this have to do with getting playergui from workspace? also for handling remotes you should probably put this under ServerScriptService (and do UI changes on client instead)

1 Like

What I’m trying to do is when this is fired stuff in PlayerGUI is changed. I’ve tried to have it change in the playergui and it bugged out if you tried to do it more then once

I’m not sure how your game is setup, but if I wanted the server to change something on a players UI, I would just have it fire a remoteevent which a localscript (which already has the UI references) would pick up on

Instead of getting the PlayerGui from the Player, you could try sending the PlayerGui as an argument.

-- Client Side
Remote:FireServer(Player, PlayerGui)

-- Server Side
leaveGuiEvent.OnServerEvent:Connect(function(player, playerGui)

he would not be able to modify the player ui however. I assume he wants that. It would be better to follow the model of: server holds and can modify data and client just reads said data.

CLOSED. Thanks for trying to help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.