attempt to index nil with 'Character'in Roblox Luau: what it means and how to fix it

What it means

You wrote something.Character, and the something was nil. Luau cannot look up a property on nothing, so the script stops on that line.

The same error also shows up as:

  • attempt to index nil with 'Parent'
  • attempt to index nil with 'Humanoid'
  • attempt to index nil with 'Name'

The smallest script that triggers it

Triggers the errorline 5: error here
-- Script in ServerScriptService
local Players = game:GetService("Players")
 
local player = Players.LocalPlayer
local character = player.Character

The fix

Fixedline 4: the fix
-- Script in ServerScriptService
local Players = game:GetService("Players")
 
Players.PlayerAdded:Connect(function(player)
  local character = player.Character or player.CharacterAdded:Wait()
  print(character.Name)
end)

Players.LocalPlayer only exists in a LocalScript. On the server it is nil, so you get each player from the PlayerAdded event.

Common causes

  1. Players.LocalPlayer used in a server Script, where it is always nil.
  2. FindFirstChild returned nil because the object was not there, and the next line used the result anyway.
  3. A function returned nothing (for example GetPlayerFromCharacter on a part that is not a character) and the result was indexed.

Practice this until it sticks

A Deep Dive on nil checks and WaitForChild: 5-minute lessons, each with a quiz. What you miss comes back in the next module. One Deep Dive a day is free. No card.