Attempt to index nil with "PlayerGui"

I’m writing a ModuleScript located in ServerScriptService to play an intro cutscene when I need it to. But it keeps erroring the following whenever I add the PlayerGui statement

ServerScriptService.IntroScript:17: attempt to index nil with 'WaitForChild'  -  Server - IntroScript:17

I’m trying to make it so the logo pops up with the camera. Here is the code.

--//Intro Script//

--[Services]
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local TweenService = game:GetService("TweenService")

--[Variables]
local PluginsFolder = ServerScriptService:WaitForChild("Plugins")
local AssetsFolder = ReplicatedStorage:WaitForChild("Assets")

local Camera = workspace.CurrentCamera

--[Player]
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

--[Required Modules]
local Utilities = require(PluginsFolder:WaitForChild("Utilities"))

local Images = require(AssetsFolder:WaitForChild("Images"))
local Audio = require(AssetsFolder:WaitForChild("Audio"))
--
local CreateObject = Utilities.CreateObject()
local IntroScript = {}

function IntroScript:PlayIntro()
	local Chunk = workspace:WaitForChild("titlescreen")
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Chunk:WaitForChild("Cutscene"):WaitForChild("cam1").CFrame
	Utilities.PlaySound(Audio.MainTheme)
	
	TweenService:Create(
		Camera,
		TweenInfo.new(4.0, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
		{CFrame = Chunk:WaitForChild("Cutscene"):WaitForChild("cam2").CFrame}):Play()
	task.wait(4.0)
	
	local Gui = Instance.new("ScreenGui")
	Gui.Parent = PlayerGui
	
	local Logo = CreateObject "ImageLabel" {
		BackgroundTransparency = 1.0,
		Image = ("rbxassetid://"..Images.Logo),
		ImageTransparency = 1.0,
		Position = UDim2.new(0.105, 0 ,0.082, 0),
		Size = UDim2.new(0.79, 0, 0.835, 0),
		AnchorPoint = Vector2.new(0.5, 0.5),
		Parent = Gui}
	TweenService:Create(Logo,
		TweenInfo.new(1.0),
		{ImageTransparency = 0.0}):Play()
	
	TweenService:Create(
		Camera,
		TweenInfo.new(1.0, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{CFrame = Chunk:WaitForChild("Camera").CFrame}):Play()
end

return IntroScript

The error happens around here:

--[Player]
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

Any help will do, thanks!

1 Like

Could you be calling the module script inside a server script? You are making a variable named “Player” by “LocalPlayer” but LocalPlayer is only accessible on Local Scripts.

LocalPlayer will be accessible on module scripts if it was called by client.

1 Like

Exactly why I’m asking if the module script is called by a server script.

The script calling and firing the function is a server script. That is probably the issue, is there any workaround I can take to make this work still with a server script?

function IntroScript:PlayIntro(Player)
    local PlayerGui = Player:WaitForChild("PlayerGui")
	local Chunk = workspace:WaitForChild("titlescreen")
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Chunk:WaitForChild("Cutscene"):WaitForChild("cam1").CFrame
	Utilities.PlaySound(Audio.MainTheme)
	
	TweenService:Create(
		Camera,
		TweenInfo.new(4.0, Enum.EasingStyle.Back, Enum.EasingDirection.Out),
		{CFrame = Chunk:WaitForChild("Cutscene"):WaitForChild("cam2").CFrame}):Play()
	task.wait(4.0)
	
	local Gui = Instance.new("ScreenGui")
	Gui.Parent = PlayerGui
	
	local Logo = CreateObject "ImageLabel" {
		BackgroundTransparency = 1.0,
		Image = ("rbxassetid://"..Images.Logo),
		ImageTransparency = 1.0,
		Position = UDim2.new(0.105, 0 ,0.082, 0),
		Size = UDim2.new(0.79, 0, 0.835, 0),
		AnchorPoint = Vector2.new(0.5, 0.5),
		Parent = Gui}
	TweenService:Create(Logo,
		TweenInfo.new(1.0),
		{ImageTransparency = 0.0}):Play()
	
	TweenService:Create(
		Camera,
		TweenInfo.new(1.0, Enum.EasingStyle.Quart, Enum.EasingDirection.Out),
		{CFrame = Chunk:WaitForChild("Camera").CFrame}):Play()
end
---
Players.PlayerAdded:Connect(function(Player)
   require(IntroModule):PlayIntro(Player)
end)

I’m not trying to play the intro first thing when the player joins in, my end goal is to have it play after everything is loaded. Is there any other workaround?

How do you require the script? (Utilize the function)

In the server script used to signal the intro playing after everything is loaded in this is the function I use.

--[Playing the Intro]
local Loaded = Instance.new("RemoteEvent")
Loaded.Parent = ReplicatedStorage
Loaded.Name = "Loaded"
Loaded.OnServerEvent:Wait()

do
	local IntroScript = require(ServerScriptService:WaitForChild("IntroScript"))
	IntroScript:PlayIntro()
	Loaded:Remove()
end

Really nothing too special.

So you use a remote event?

local Player 
local Loaded = Instance.new("RemoteEvent")
Loaded.Parent = ReplicatedStorage
Loaded.Name = "Loaded"
Loaded.OnServerEvent:Connect(function(PlayerReal)
  Player = PlayerReal
end)
Loaded.OnServerEvent:Wait()
do
	local IntroScript = require(ServerScriptService:WaitForChild("IntroScript"))
	IntroScript:PlayIntro(Player)
	Loaded:Remove()
end

Then just the function with the player argument in the function I posted earlier.

–[Player]
local Players = game:GetService(“Players”)
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui

i dont think you need to wait for it to be there, as its automatically placed

I was experimenting with that and when I print the player statement from the module it returns nil. Why is that happening?

1 Like

i got a question is your script a server script or localscript?

1 Like

You cannot use the Players.LocalPlayer inside a Normal Script. Players.LocalPlayer can only be used in clientside scripts.

you can’t use game.Players.LocalPlayer in a server script, you have to get the player in a different way

ik but is ths guy using serverscript?

A lot of testing later and this works! Thanks for your time.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.