Why is this script erroring?

Im trying to make a script that will get the players GUI so It can tell them what to do, the script keeps giving off an error though;

  12:24:59.598  ServerScriptService.MainScript:8: attempt to index nil with 'ScreenGui'  -  Server - MainScript:8
  12:24:59.598  Stack Begin  -  Studio
  12:24:59.598  Script 'ServerScriptService.MainScript', Line 8  -  Studio - MainScript:8
  12:24:59.598  Stack End  -  Studio

Here is the Script;

-- Obtaining the PlayerGui
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		PlayerGui = player:WaitForChild("PlayerGui")
	end)
end)

-- Defining Variables
local TutorialText = PlayerGui.ScreenGui.TutorialText
local BaseplateStrinkScript = game.Workspace.Baseplate.Script
local CarSpawnerScript = game.Workspace.Machine.CarSpawner.Gen
local kickHandlerScript = game.ServerScriptService.KickHandler
local playersInServer = #game.Players:GetPlayers()

-- First Line Of Code
BaseplateStrinkScript.Disabled = true
CarSpawnerScript.Disabled = true
kickHandlerScript.Disabled = true

-- Game Startup
local function StartTheGame()
	kickHandlerScript.Disabled = false
	wait(5)
	PlayerGui.TutorialText.Visible = true
	wait(4)
	PlayerGui.TutorialText.Text = "In this game, Players must use the cars and Knock eachother off the Baseplate!"
	wait(7)
	PlayerGui.TutorialText.Text = "The Baseplate Will Strink Slowly so make sure you dont fall!"
	wait(7)
	PlayerGui.TutorialText.Text = "The Last player Standing Wins!"
	wait(5)
	PlayerGui.TutorialText.Text = "Goodluck, Players!"
	wait(5)
	PlayerGui.TutorialText.Visible = false
	BaseplateStrinkScript.Disabled = false
	CarSpawnerScript.Disabled = false
end

-- Looking For The Winner
local function StopTheGameAWinnerHasBeenChosen()
	BaseplateStrinkScript.Disabled = true
	wait(5)
	PlayerGui.TutorialText.Visible = true
	PlayerGui.TutorialText.Text = "Great Job Player!"
	wait(4)
	PlayerGui.TutorialText.Text = "Looks Like you have Won the game!"
	wait(5)
	PlayerGui.TutorialText.Text = "Your now one of the Best Fighters In Robloxia!"
	wait(7)
	PlayerGui.TutorialText.Text = "You were truley a Bloxer, you Deserve This Medal!"
	wait(5)
	PlayerGui.TutorialText.Text = "Congrats and, Goodbye!"
end

game.Players.PlayerRemoving:Connect(function()
	if #game.Players:GetPlayers() == 1 then
		StopTheGameAWinnerHasBeenChosen()
	end
end)

game.Players.PlayerRemoving:Connect(function()
	if #game.Players:GetPlayers() == math.random(2,20) then -- Checks if the playerlist has 2 all of the way to 20 players.
		StartTheGame()
	end
end)

PlayerGui is nil because the script doesn’t wait for the player to join, since its an event. I do believe this is a pretty bad approach, though, as if another player joins then the PlayerGUI variable will be overwritten. Also, unless this is only a snippet of your code, I don’t see you actually define PlayerGui anywhere, except for in the scope .

1 Like

Would this work?

local PlayerGui = game.Players.PlayerAdded:Connect(function(player)
	player:WaitForChild("PlayerGui")
end)