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

Attempt to index local 'gui' (a nil value?)

Please help, I'm trying to get a GUI to open when a tool is selected but it isn't opening at all. :-(

I get this error when I open the tool: OpenGUI:9: Attempt to index local 'gui' (a nil value), Line 9 - global LoadGui, Line 23

And this error when I close the tool: BenOVGui is not a valid member of PlayerGui, Line 18 - global UnloadGui, Line 18, Line 27

local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

function LoadGui()
    repeat wait() until player.Character 
    if not player.PlayerGui:FindFirstChild("BenOVGUI") then
        local cgui = game:GetService("ReplicatedStorage"):WaitForChild("BenOVGUI")
        gui = cgui:Clone()
        gui.Parent = playerGui <--
        print("Gui placed in ".. player.Name) 
    else 
        player.PlayerGui.BenOVGUI.Enabled = true
        print("Gui Enabled.")
    end
end

function UnloadGui()
    playerGui.BenOVGUI.Enabled = false <--
    print("Gui Disabled.")
end

script.Parent.Parent.Equipped:connect(function()
    LoadGui() <--?
end)

script.Parent.Parent.Unequipped:connect(function()
    UnloadGui() <--?
end)

5 comments

0

Please help?

0

When I ran the code, it worked perfectly fine. Try restarting studio maybe.

0

Really? In studio it shows no errors and does nothing. In game, it shows those errors.. :/

0

I just tried it in game and it works fine too.

0

I'm using Adonis Admin and it screwed with the way I had it before. I'm getting the errors with it but I really like the admin. If you add it in you'll get the errors.

2 answers

Um, well here's a more efficient code that I used:

local replicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
playerGui = player.PlayerGui
local tool = script.Parent

function LoadGui()
	local BenOVGUI = playerGui:FindFirstChild("BenOVGUI")
	if BenOVGUI then
		BenOVGUI.Enabled = true
	else
		local cgui = replicatedStorage:WaitForChild("BenOVGUI")
		local gui = cgui:Clone()
		gui.Parent = playerGui
		gui.Enabled = true
	end
end

function UnloadGui()
	local BenOVGUI = playerGui:FindFirstChild("BenOVGUI")
	if BenOVGUI then
		BenOVGUI.Enabled = false
	end
end

tool.Equipped:connect(function(mouse)
	LoadGui()
end)

tool.Unequipped:connect(function()
	UnloadGui()
end)

I've tested it out and had no problems, so it could really either be that your object directories are wrong or the gui is named differently.

7 comments

0

Yes, it has a part called Handle.

0

I'm using Adonis Admin and it screwed with the way I had it before. I'm getting the errors with it but I really like the admin. If you add it in you'll get the errors.

0

Edited ^

0

I'm getting the attempt to index local 'gui' (a nil value) error with Line 15 - global LoadGui, Line 28

0

Please help!

0

That means the script couldn't find anything named BenOVGUI in the ReplicatedStorage. Try checking if your directories and names are word for word correct

0

Except.. there is a gui named BenOVGUI in ReplicatedStorage.

If you mean when a tool is equipped, i think this will work :-) put this in a localscript in startergui and change line 2 to the gui you want to make visible and change BigSword to the tool name

game.Workspace:WaitForChild(game.Players.LocalPlayer.Name):WaitForChild("BigSword") --Change to tool name
script.Parent.ScreenGui.TextLabel.Visible = true

Hope it works, if not tell me and ill try to fix it :)

1 comment

0

this should work, why dont you use it?

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