Why can't i access player gui?

Is there something wrong with my code? I already tried to get help from other places before, they programmed in the dame thing as me, and I’m the one who gets the error. This is the code:

local Player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(hit)
	local PlrGui = Player.PlayerGui
	PlrGui:FindFirstChild("Sound")
	PlrGui.Sound:Destroy()
end)
1 Like

Maybe, try to 1st print the PlayerGui, if it says something like: PlayerGui.

Add a script, beneath the script (must be a localscript!).

local Player = game.Players:FindFirstChildOfClass("Player")

script.Parent.Touched:connect(function()
	script.LocalScript.Enabled = true and script.Enabled == false -- Why is there "==" on script.Enabled? Because when you do only once it'll error in the script analysis, automatically, it'll error on the output.
end)

On this localscript, put

local Player = game.Players.LocalPlayer
local PlrGui = Player.PlayerGui
PlrGui:WaitForChild("Sound")
PlrGui.Sound:Destroy()
script.Parent.Enabled = true and script.Enabled == false -- Why is there "==" on script.Enabled? Because when you do only once it'll error in the script analysis, automatically, it'll error on the output.

Also, put it on #help-and-feedback:scripting-support, it’s the main topic subject that contains errors on the output/no errors but doesn’t function at all.

I got this error now:

Send me a localfile, I can’t think of a word to say into my brain.

I’m sorry, I don’t know what a localFile is.

A localfile is that you save it and it automatically makes it as the file of “rbxl”.

No I mean I don’t know why I keep getting the error

Send me a localfile immediately, otherwise the bug will never get fixed (kind of).

You need to check to see if the hit is the player:

script.Parent.Touched:connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
	     local PlrGui = player.PlayerGui
	     PlrGui.Sound:Destroy()
    end
end)
2 Likes
local Player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(hit)
	local PlrGui = Player:WaitForChild("PlayerGui")
	local Sound = PlrGui:FindFirstChild("Sound")
    if not Sound then return end
	Sound:Destroy()
end)

Make sure the script is a localscript.

For some reason, nothing happened with the LocalScript, but i keep getting the same error every time with the Regular Script!

My script didn’t work for you? It should be in the part that you touch.

It was in a separate part away from a different part that would play the sound.

Have you tried putting this script (local script) in the StarterPlayerScripts, and not in workspace with the part.

I don’t think local scripts work in workspace. Change this line in the local script: script.Parent.Touched to the path of the part if you want to use a local script.

If you want to keep it in a server script and the same location, try this:


script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
	local PlrGui = game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui
	PlrGui:FindFirstChild("Sound")
	PlrGui.Sound:Destroy()
end
end)

Here is some info on why you got the Errors you had:

Summary
  • The local Player doesn’t exist on the server and so, doesn’t exist on server scripts (or the normal scripts).

  • Local Scripts do not fire when they are in workspace, so parenting it to any part in workpace will never work

1 Like

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