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)
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.