Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is 'player' nil?

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?

1 comment

0

try doing playergui = player:WaitForChild("PlayerGui")

3 answers

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.

4 comments

0

that didnt work

0

try llocal p = game:WaitForChild("Players") local player = p.LocalPlayer local playerGui = player.PlayerGui

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

0

I personally don't see how this would work. I wish you had gone into details for your conclusion.

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.

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)

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