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