16:28:55.068 ServerScriptService.GameHandler:21: attempt to index nil with 'FindFirstChild' - Server - GameHandler:21, how to fix?

I got this error when I was testing my events, why is this occuring?
‘’'lua
local GameData = game.ReplicatedStorage.GameData
local Assets = game.ServerStorage.Assets
local Folder = game.Workspace.Folder
local TweenService = game:GetService(“TweenService”)
local Status = GameData.Status

local Settings = {
Intermission = 10,
PlayersNeeded = 1,
TimeBetweenEvents = 5,
RoundInProgress = GameData.RoundInProgress.Value
}

local function CalculatePlayers()
local NumberOfPlayers = #game.Players:GetPlayers()
return NumberOfPlayers
end

local function Events(player)
local RandomEvent = math.random(1,5)
local Plate = player.Character:FindFirstChild(“PlateValue”).Value

if RandomEvent == 1 then
	Status.Value = player.Name.. " Plate will turn green"
	Plate.PartColor = BrickColor.new("Shamrock")
elseif RandomEvent == 2 then
	local Shrink = math.random(20, 85)
	Status.Value = player.Name.. " plate will be crushed "..Shrink.. " studs."
	local ShrinkInfo = TweenInfo.new(
		0.75,
		Enum.EarningStyle.Linear,
		Enum.EarningDirection.Out,
		0,
		false,
		0
	)
	local Tween = TweenService:Create(Plate, ShrinkInfo, (Plate.Size - Vector3.new(Shrink, 0, Shrink))):Play()
elseif RandomEvent == 3 then
	local Grow = math.random(20, 85)
	Status.Value = player.Name.. " plate will be gifted with "..Grow.. " studs."
	local ShrinkInfo = TweenInfo.new(
		0.75,
		Enum.EarningStyle.Linear,
		Enum.EarningDirection.Out,
		0,
		false,
		0
	)
	local Tween = TweenService:Create(Plate, ShrinkInfo, (Plate.Size - Vector3.new(Grow, 0, Grow))):Play()
elseif RandomEvent == 4 then
	Status.Value = player.Name.. " will be dried up to dust"
	Plate.Material = "Sand"
elseif RandomEvent == 5 then
	Status.Value = player.Name.. " will get a drooling nuisance"
	local Zombie = Assets["Drooling Zombie"]:Clone()
	Zombie.Parent = game.Workspace
	Zombie.HumanoidRootPart.Cframe = Plate.CFrame * CFrame.new(0, 2, 0)
end

end

while wait() do
if CalculatePlayers() >= Settings.PlayersNeeded then
for i, v in pairs(Folder:GetChildren()) do
if v then
v:Destroy()
end
end

	local PlayersInGame = {}
	-- where the game starts
	for i = Settings.Intermission, 1, -1 do
		Status.Value = "intermission - "..i
	end
	
	Status.Value = "The Round is ready to begin!"
	Settings.RoundInProgress = true
	wait(5)
	local Plates = Assets.Plates:Clone()
	Plates.Parent = game.Workspace.Folder
	
	for keyvalue, player in pairs(game.Players:GetPlayers()) do
		if player.Character:FindFirstChild("Humanoid") then
			if player.Character.Humanoid.Health >=1 then
				table.insert(PlayersInGame, keyvalue, player)
				player.Team = game.Teams.Survivors
				for i, v in pairs(Plates:GetChildren()) do
					if v.Settings.OwnerOfPlate.Value == "None" and not player.Character:FindFirstChild("PlateValue") then 
						v.Settings.OwnerOfPlate.Value = player.Name
						player.Character.HumanoidRootPart.CFrame = v.CFrame * CFrame.new(0, 2, 0)
						local PlateValue = Instance.new("ObjectValue")
						PlateValue.Parent = player.Character
						PlateValue.Value = v
						PlateValue.Name = "PlateValue"
					end
				end
			end
		end
	end
	
	for i, v in pairs(Plates:GetChildren()) do
		if v.Settings.OwnerOfPlate.Value == "None" then
			v:Destroy()
		end
	end
	
	wait(5)
	
	repeat
		for i = Settings.TimeBetweenEvents, 1, -1 do
			Status.Value = "Next Event is in "..i.. " seconds!"
			wait(1)
		end
		if #PlayersInGame >= 1 then
			local Value = math.random(1, #PlayersInGame)
			local RandomPlayer = PlayersInGame
			
			Events(RandomPlayer)
			wait(5)
		else
			for i, v in pairs(PlayersInGame) do
				table.remove(PlayersInGame, 1)
				
			end
		end
	until Settings.RoundInProgress == false
else 
	-- not enough players
	Status.Value = "At least two players are needed to anger the gods!"
end

end
‘’’

this was the line supposedly causing the issue.

First and for most, I find this post be lazy just by pasting your error log in the tittle and pasting your entire code. In turn, makes it time consuming to read your code for something so basic.

local Plate = player.Character:FindFirstChild(“PlateValue”).Value

player.Character is nill.

I believe the function is running before your character is even created. Or at the time this line is executed the character is not created or loaded in. To get around this you must wait for the character.

Could be a more simpler way of doing this but I’m lazy. Place this above your Variable named “Plate”:

player:LoadCharacter()
local Character = player.Character
repeat 
    Character = player.Character
    wait() 
until character ~= nil

Or you can just load the character to ensure there is one by using “Player:LoadCharacter()”
But then again you always have to make sure that there is a character loaded.

1 Like

Let me know if this helps. And also most of your variables and service variables may have this same issue. When a server/instance is started, all of your objects may not be already loaded in. So your script could start running before local GameData = game.ReplicatedStorage.GameData is even created or loaded in.

To get around this; most people would use game:GetService("ReplicatedStorage"):WaitForChild("GameData") to stall the script execution until that service is created or that line creates te service itself.

As for setting variables with object values, you would need a :WaitForChild() for each object in your family tree. For example I wanted a part in the workspace with in a model I would use:

local Part = workspace:WaitForChild("Model"):WaitForChild("Part")

May seem tedious but this would help you with a lot things. As for creating variables within function this is not needed.

You can replace this with:

local Character = player.Character or player.CharacterAdded:Wait()
3 Likes