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

My GUi Script is failing and I don't know what it's talking about, keep getting nil error?

Asked by 4 years ago

When I try to open my book here is what it says and here is my script, and I'm getting frustrated!

local book = game:GetService('Workspace').Table.Book.ClickDetector
local close = PlayerGui.Book.Close
local next1 = PlayerGui.Book.Next1
local back1 = PlayerGui.Book.Back1

book.MouseClick:Connect(function()
    PlayerGui.Book.Enabled = true
end)


close.MouseButton1Click:Connect(function()
    PlayerGui.Book.Enabled = false
end)

next1.MouseButton1Click:Connect(function()
    PlayerGui.Book.Close.Visible = false
    PlayerGui.Book.Close.Selectable = false
    PlayerGui.Book.Blood.Visible = false
    PlayerGui.Book.Pyro.Visible = false
    PlayerGui.Book.Pain.Visible = true
    back1.Visible = true
    back1.Selectable = true
    next1.Visible = false
    next1.Selectable = false

The error says 'Players.JaytheKing452.PlayerGui.Book.BookScript:8: attempt to index global 'PlayerGui' (a nil value)'

1 answer

Log in to vote
0
Answered by 4 years ago

Ahh I see the problem, you are forgetting to reference the local player. Each player has their own Screen GUI, which means the local player has to be referenced before accessing the PlayerGui. It would go something like this!

local plr = game:GetService('Players').LocalPlayer
local book = game:GetService('Workspace').Table.Book.ClickDetector
local close = plr.PlayerGui.Book.Close
local next1 = plr.PlayerGui.Book.Next1
local back1 = plr.PlayerGui.Book.Back1

book.MouseClick:Connect(function()
    plr.PlayerGui.Book.Enabled = true
end)


close.MouseButton1Click:Connect(function()
    plr.PlayerGui.Book.Enabled = false
end)

next1.MouseButton1Click:Connect(function()
    plr.PlayerGui.Book.Close.Visible = false
    plr.PlayerGui.Book.Close.Selectable = false
    plr.PlayerGui.Book.Blood.Visible = false
    plr.PlayerGui.Book.Pyro.Visible = false
    plr.PlayerGui.Book.Pain.Visible = true
    back1.Visible = true
    back1.Selectable = true
    next1.Visible = false
    next1.Selectable = false

Do you see how I am referencing the local player before gui, just remember that!

0
ill test it ourt right now JaytheKing452 9 — 4y
0
Works THANKS! JaytheKing452 9 — 4y
Ad

Answer this question