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

Can Global Scripts Access PlayerGui?

I have a global script that is supposed to change some properties of a player's GUI's, but it isn't working. I'm assuming it has something to do with the server/client boundary but, even if it is, I don't know how to fix it. Any help would be appreciated.

local part = script.Parent
local chatting = false

function onClicked(plr)
	local dialog = plr.PlayerGui.NPCDialog.DialogFrame
	local words = dialog.Chat.Text
	local name = dialog.NPCName.Text
	if chatting == false then
		chatting = true
		print("Chatting with NPC") 
		name = script:FindFirstAncestorOfClass("Model").Name
		words = "Ey b0ss"
		dialog.Visible = true -- displays the chat
	end
end
part.ClickDetector.MouseClick:connect(onClicked)

This script successfully makes the GUI visible, but doesn't change the text as it should.

1 answer

Yes, but no.

The server can access PlayerGui itself but not the descendants as the contents of StarterGui are cloned locally into the player's PlayerGui. Unless the server put them in itself but the server shouldn't be changing things only one player will see.

What you should do is make this a local script, and move the local script under the UI you want to change.

Also, it won't change the text like that, you're only setting your variable to the text at the time the script ran, not the property itself.

local part = game:GetService("Workspace").Part -- change this
local chatting = false
local client = game:GetService("Players").LocalPlayer

part.ClickDetector.MouseClick:Connect(function(_client) -- :connect is deprecated use :Connect
    if client ~= _client then
        return
    end
    local dialog = client.PlayerGui.NPCDialog.DialogFrame
    local words = dialog.Chat
    local name = dialog.NPCName

    if not chatting then
        chatting = true
        print("Chatting with NPC") 
        name.Text = script:FindFirstAncestorOfClass("Model").Name
        words.Text = "Ey b0ss"
        dialog.Visible = true -- displays the chat
    end
end)

The check if it's the local player who clicked is important since MouseClick on the client won't fire only for the local player

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