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

Why is 'player' nil?

Asked by 8 years ago
local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local htp = playergui:WaitForChild("Main").Main.HowToPlay
local main = playergui:WaitForChild("Main").Main.Main
local leaderboards = playergui:WaitForChild("Main").Main.LeaderboardsFrame

main.HowToPlay.MouseButton1Down:connect(function()
    htp.Visible = true
    main.Visible = false
end)

htp.Back.MouseButton1Down:connect(function()
    htp.Visible = false
    main.Visible = true
end)

leaderboards.Back.MouseButton1Down:connect(function()
    leaderboards.Visible = false
    main.Visible = true
end)

main.Leaderboards.MouseButton1Down:connect(function()
    main.Visible = false
    leaderboards.Visible = true
end)

Error: line 2, attempt to index local 'player' (a nil value)

This script is a LocalScript placed in a GUI, this works in Studio on Solo Play, but not in a Server?

0
try doing playergui = player:WaitForChild("PlayerGui") QuantumToast 261 — 8y

3 answers

Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
8 years ago

So, not gonna go into all the detail; however, what is most likely happening is that the script is running before the player loads. Yea, I know it sounds weird, but it happens.

Edit:

Try using:

repeat wait() until game.Players.LocalPlayer

on the first line, as this will wait until said object is found.

0
that didnt work thehybrid576 294 — 8y
0
try llocal p = game:WaitForChild("Players") local player = p.LocalPlayer local playerGui = player.PlayerGui NRCme 0 — 8y
0
^ I made some variables this should work it worked for me but what it is doing is waiting until that's created completely P.S This is 3 different variables NRCme 0 — 8y
0
I personally don't see how this would work. I wish you had gone into details for your conclusion. User#11440 120 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Maybe using script.Parent would work. It is better than just link it from game. Here is how it works:

local playergui = script.Parent.Parent -- I supposed it is in the ScreenGui...
local htp = playergui:WaitForChild("Main").Main.HowToPlay
local main = playergui:WaitForChild("Main").Main.Main
local leaderboards = playergui:WaitForChild("Main").Main.LeaderboardsFrame

main.HowToPlay.MouseButton1Down:connect(function()
    htp.Visible = true
    main.Visible = false
end)

htp.Back.MouseButton1Down:connect(function()
    htp.Visible = false
    main.Visible = true
end)

leaderboards.Back.MouseButton1Down:connect(function()
    leaderboards.Visible = false
    main.Visible = true
end)

main.Leaderboards.MouseButton1Down:connect(function()
    main.Visible = false
    leaderboards.Visible = true
end)

Don't worry! The script will know how it is linked with script.Parent.Parent. Its first parent is the ScreenGui and the second parent is the LocalPlayer's PlayerGui! This is why using script.Parent in lua coding is really useful! :) If you have any problems, leave it in the comments! I hope this will help.

Log in to vote
0
Answered by
Uglypoe 557 Donator Moderation Voter
8 years ago

It's running too fast. A wait() should be all you need.

wait()
local player = game.Players.LocalPlayer
local playergui = player.PlayerGui
local htp = playergui:WaitForChild("Main").Main.HowToPlay
local main = playergui:WaitForChild("Main").Main.Main
local leaderboards = playergui:WaitForChild("Main").Main.LeaderboardsFrame

main.HowToPlay.MouseButton1Down:connect(function()
    htp.Visible = true
    main.Visible = false
end)

htp.Back.MouseButton1Down:connect(function()
    htp.Visible = false
    main.Visible = true
end)

leaderboards.Back.MouseButton1Down:connect(function()
    leaderboards.Visible = false
    main.Visible = true
end)

main.Leaderboards.MouseButton1Down:connect(function()
    main.Visible = false
    leaderboards.Visible = true
end)

Answer this question