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:7: Attempt to index local 'gui' (a nil value), global LoadGui OpenGUI: Line 21

And this error when I close the tool: OpenGUI:16: Attempt to index local 'gui' (a nil value), global UnloadGui: Line 25

local player = game.Players.LocalPlayer

function LoadGui()
repeat wait() until player.Character 
if not player.PlayerGui:findFirstChild("BenOVGUI") then
gui = game.ReplicatedStorage.BenOVGUI:Clone()
gui.Parent = game.Players.LocalPlayer.PlayerGui
print("Gui placed in ".. player.Name) 
else 
gui.Enabled = true
print("Gui Enabled.")
end
end

function UnloadGui()
gui.Enabled = false
print("Gui Disabled.")
end

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

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

3 comments

0

The clone might be incorrect.

0

What do you mean?

0

Make sure BenOVGUI exists in ReplicatedStorage (Case sensitive). Also, use game:GetService("ReplicatedStorage") instead of game.ReplicatedStorage.

1 answer

Try this code instead, make sure you have the GUI in ReplicatedStorage.

(uhh I used tabs because it looks prettier)

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)

Let me know if you have problems, I'll try to help gladly.

6 comments

0

Got rid of those errors, but now there is this one: "Equipped is not a valid member of Backpack, Line 22"

0

If this script is a child of a tool, try doing script.Parent.Equipped and script.Parent.Unequipped in lines 22 and 26, respectively.

0

It is, forgot to do that. Now there are these errors: "OpenGUI:9: attempt to index global 'gui' (a nil value), Line 9 - global UnloadGui, Line 23 ---- BenOVGui is not a valid member of PlayerGui, Line 18 - global UnloadGui, Line 18, Line 27

0

Hello?

0

oh hi

0

Hi.. I'm having problems still and need help..? :-(

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