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

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

Asked by 6 years ago

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)
0
The clone might be incorrect. Kulh 125 — 6y
0
What do you mean? LuckyAura -1 — 6y
0
Make sure BenOVGUI exists in ReplicatedStorage (Case sensitive). Also, use game:GetService("ReplicatedStorage") instead of game.ReplicatedStorage. shayner32 478 — 6y

1 answer

Log in to vote
0
Answered by
Sur4y 40
6 years ago

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.

0
Got rid of those errors, but now there is this one: "Equipped is not a valid member of Backpack, Line 22" LuckyAura -1 — 6y
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. Sur4y 40 — 6y
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 LuckyAura -1 — 6y
0
Hello? LuckyAura -1 — 6y
View all comments (2 more)
0
oh hi Sur4y 40 — 6y
0
Hi.. I'm having problems still and need help..? :-( LuckyAura -1 — 6y
Ad

Answer this question