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

attempt to index nil with 'PlayerGui' , how do i fix it?

i am trying to make the gui disappear when clicked

script.Parent.MouseButton1Click:connect(function()
	script.Parent.Parent.v4.Visible = true
	player.PlayerGui.ScreenGui.a1.Visible = false
	player.PlayerGui.ScreenGui.a2.Visible = false
	player.PlayerGui.ScreenGui.a3.Visible = false
	player.PlayerGui.ScreenGui.a4.Visible = false
	player.PlayerGui.ScreenGui.q.Visible = false
	player.PlayerGui.ScreenGui.w.Visible = false
	player.PlayerGui.ScreenGui.y.Visible = false
	player.PlayerGui.ScreenGui.z.Visible = false

end)

1 comment

0

Please format your code correctly, it's so much easier to read then

3 answers

✓ Accepted answer

#Answer:

Step-By-Step explanation

##What does this error mean?

The error message attempt to index nil, means that you've attempted to index something that does not exist in the workspace. In this case it's the player.

Quick Tip: Errors are usually very easy to deal with, just follow what the error code says and the make some refinements, IF there's no errors obviously try and find out what's wrong with the script, you can do this by using the function print() (again as stated, if there's no errors enumerated above).

If you'd like, you can visit this article to learn about prints and strings to help you proofread your script by clicking on the hyperlinks, or you can watch a short video by ROBLOX themselves on how to use prints for debugging.

##How can this be resolved

You simply have to define/reference the local player!

##Fixed Script

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
    script.Parent.Parent.v4.Visible = true
    player.PlayerGui.ScreenGui.a1.Visible = false
    player.PlayerGui.ScreenGui.a2.Visible = false
    player.PlayerGui.ScreenGui.a3.Visible = false
    player.PlayerGui.ScreenGui.a4.Visible = false
    player.PlayerGui.ScreenGui.q.Visible = false
    player.PlayerGui.ScreenGui.w.Visible = false
    player.PlayerGui.ScreenGui.y.Visible = false
    player.PlayerGui.ScreenGui.z.Visible = false
end)

This should fix your issue!

####Re-edited by JesseSong!

If you have any further enquiries don't hesitate to comment!

1 comment

0

Re-edited by JesseSong!

The Problem is, based on the info you have provided, you aren't defining the player. There are 2 ways to define the player:


IN A SERVER SCRIPT


Because the server script is ran on the server, the server can't identify what player is automatically, as it handles multiple clients. Instead, you can access the player through the Player.PlayerAdded Event. This event runs when a player joins the server and becomes a client. You can then save the player who joined to a variable like so:

game.Players.PlayerAdded:Connect(function(Player)
	---Do Stuff
end)

IN A LOCAL SCRIPT


A local script is ran on the client, so it is easier to identify who player actually is. You can access the player by simply doing this:

local Player = game.Players.LocalPlayer

Hope this helps

In the script you have provided, you have not defined player! To define the player in a localscript, I suggest using LocalPlayer, like so:

local player = game.Players.LocalPlayer

This would simply go on top of your script, like so:

local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:connect(function()
    script.Parent.Parent.v4.Visible = true
    player.PlayerGui.ScreenGui.a1.Visible = false
    player.PlayerGui.ScreenGui.a2.Visible = false
    player.PlayerGui.ScreenGui.a3.Visible = false
    player.PlayerGui.ScreenGui.a4.Visible = false
    player.PlayerGui.ScreenGui.q.Visible = false
    player.PlayerGui.ScreenGui.w.Visible = false
    player.PlayerGui.ScreenGui.y.Visible = false
    player.PlayerGui.ScreenGui.z.Visible = false
end)

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