Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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)

0
Please format your code correctly, it's so much easier to read then JeffTheEpicRobloxian 258 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago
Edited by JesseSong 4 years ago

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 veryeasy 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!

0
Re-edited by JesseSong! JesseSong 3916 — 4y
Ad
Log in to vote
2
Answered by 4 years ago

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

Log in to vote
1
Answered by
ben0h555 417 Moderation Voter
4 years ago

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)

Answer this question