Fix "Attempt to Index Nil With" Errors in Roblox Studio
What the Roblox "attempt to index nil with" error means, how to find the nil value in your script, and fixes for the six causes that trigger it most often.
On this page
- What “attempt to index nil with” actually means
- How to find which value is nil
- Cause 1: The character hasn’t loaded yet
- Cause 2: FindFirstChild returned nil
- Cause 3: A Touched hit that isn’t a player
- Cause 4: LocalPlayer in a server Script
- Cause 5: A raycast that hit nothing
- Cause 6: A model with no PrimaryPart
- Which fix to use
- If it still doesn’t work
- Related guides
- Sources
The Roblox error “attempt to index nil with” means your script tried to read something from a value that doesn’t exist. The quoted name at the end of the message is what you asked for, and the real problem is the object just before it. This guide shows how to find that nil value and fix the most common causes.
What “attempt to index nil with” actually means
In Luau, indexing means reaching into a value with ., :, or [], for example part.Position or character:FindFirstChild("Humanoid"). The Luau docs describe nil as the value that stands for “nothing”, so there is nothing to reach into. When you try anyway, the script stops with an error like this:
ServerScriptService.Script:12: attempt to index nil with 'Humanoid'
Read it in three parts:
ServerScriptService.Scriptis the script that failed.12is the line number.'Humanoid'is the name you tried to read.Humanoiditself isn’t missing. The value you were reading it from wasnil.
So if line 12 is local humanoid = character.Humanoid, the nil value is character, not Humanoid.
How to find which value is nil
When a line has a long chain like workspace.Map.Door.Hinge.CFrame, any link could be the nil one. Two quick ways to find it:
-
Print each piece just before the failing line:
print("map:", workspace:FindFirstChild("Map"))A value that prints
nilis your culprit. -
Pause on the line with a breakpoint. Click in the gutter next to the line number in the Script Editor, start a playtest, and open Window › Debug › Watch. The Variables tab shows the value of every variable in scope when the script pauses.
Once you know which variable is nil, ask why it’s nil. The causes below cover most cases.
flowchart TD
A["Read the error line number"] --> B["Find the value left of the quoted name"]
B --> C{"Should it exist yet?"}
C -->|"Not loaded yet"| D["Wait for it"]
C -->|"May never exist"| E["Check for nil first"]
C -->|"Should exist now"| F["Fix the name, path, or script type"]
Cause 1: The character hasn’t loaded yet
This is the most common source of attempt to index nil with 'Humanoid'. According to the Player reference, Player.Character starts as nil and is only set when the character first spawns. A script that runs as soon as the player joins can get there first.
Wait for the character instead of assuming it exists:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
How it works:
player.Character or ...uses the character if it already exists.- If it’s
nil,player.CharacterAdded:Wait()pauses the script until theCharacterAddedevent fires and returns the new character. WaitForChild("Humanoid")then waits for the Humanoid inside it, because the model can arrive before all of its children.
The Creator Hub uses this same pattern in its Player code samples. Remember that the character is replaced every time the player respawns. If your script keeps a reference, update it in a CharacterAdded connection.
Cause 2: FindFirstChild returned nil
FindFirstChild returns the child with that name, or nil if no such child exists. If you index the result right away, a missing child turns into this error:
-- Errors if there is no "Door" in Workspace
workspace:FindFirstChild("Door").Transparency = 0.5
Check the result before you use it:
local part = workspace:FindFirstChild("Door")
if not part then
warn("Door is missing")
return
end
part.Transparency = 0.5
if not part then catches nil, warn writes a message to the Output window, and return stops the function before the bad line. If the child should exist, check the name and parent in the Explorer. A typo, different capitalization, or an object nested one folder deeper than you expected all return nil.
If the object is created or replicated a moment later, use WaitForChild instead. Note that WaitForChild with a timeout also returns nil when time runs out, so check that result too. Our guide to the Infinite yield possible warning covers that case in detail.
Cause 3: A Touched hit that isn’t a player
Touched fires for any part, not just characters. Code like this breaks as soon as a falling brick hits your part:
local Players = game:GetService("Players")
local pad = script.Parent
pad.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if not player then
return -- not a player's character
end
print(player.Name .. " touched the pad")
end)
GetPlayerFromCharacter returns the player whose character matches the model, or nil if there isn’t one. Without the if not player check, player.Name would raise “attempt to index nil with ‘Name’” every time something other than a character touches the pad. The same applies if you look up a Humanoid with hit.Parent:FindFirstChildOfClass("Humanoid"): check the result before using it.
Cause 4: LocalPlayer in a server Script
The Players reference is clear: on the server, Players.LocalPlayer is nil. It only has a value in LocalScripts (and ModuleScripts required by them). So Players.LocalPlayer.leaderstats in a regular Script fails with “attempt to index nil with ‘leaderstats’”.
Fix it by getting the player another way on the server, for example from Players.PlayerAdded, from GetPlayerFromCharacter, or from the first argument of RemoteEvent.OnServerEvent. If the code really belongs on the client, move it into a LocalScript.
Cause 5: A raycast that hit nothing
workspace:Raycast() returns a result object, or nil if the ray didn’t hit any eligible part or terrain. Reading .Instance or .Position from a miss fails:
local origin = Vector3.new(0, 50, 0)
local direction = Vector3.new(0, -100, 0)
local result = workspace:Raycast(origin, direction)
if result then
print("Hit", result.Instance.Name, "at", result.Position)
else
print("The ray hit nothing")
end
The ray starts 50 studs up and points 100 studs down. The if result then check handles the miss case instead of erroring.
Cause 6: A model with no PrimaryPart
Models don’t have PrimaryPart set by default, according to the Model reference. Code like model.PrimaryPart.Position then fails with “attempt to index nil with ‘Position’”.
You have two options:
- Set the PrimaryPart property on the model in the Properties window (or in code) before you use it.
- Use
model:GetPivot()andmodel:PivotTo(), which work whether or not a PrimaryPart is set. The Creator Hub recommends them over the olderGetPrimaryPartCFrameandSetPrimaryPartCFramemethods.
Which fix to use
| Situation | Typical error | Fix |
|---|---|---|
| Character not spawned yet | with 'Humanoid' | player.Character or player.CharacterAdded:Wait() |
| Child may not exist | with 'Transparency', with 'Value' | Check the FindFirstChild result for nil |
| Child will appear soon | Any child name | WaitForChild, and handle a timeout’s nil |
| Touched by a non-character | with 'Name', with 'Humanoid' | Check GetPlayerFromCharacter for nil |
| Server Script uses LocalPlayer | with 'leaderstats', with 'Character' | Get the player from an event, or use a LocalScript |
| Raycast missed | with 'Instance', with 'Position' | if result then |
| Model has no PrimaryPart | with 'Position', with 'CFrame' | Set PrimaryPart or use GetPivot() |
If it still doesn’t work
- The error points to a CoreGui or CorePackages script. That code belongs to Roblox, not your experience. If none of your scripts appear in the stack trace, there may be nothing in your code to fix.
- It only fails in a live server. Check whether the code runs in a Script or a LocalScript, and that objects your LocalScripts need aren’t stored in ServerStorage or ServerScriptService, which clients can’t see.
- It happens after a respawn. A variable that stored the old character now points to a destroyed model. Grab the new character in a
CharacterAddedhandler. - It’s a table, not an Instance. The same error appears for plain tables, for example
data.Coinswhendatawas never set. Print the table before the line to confirm what it holds.
Every example here uses documented Creator Hub APIs, but test each change in Studio with your own scripts, since your hierarchy and names will differ. Once you know which value is nil and why, the fix is usually a single line.
Related guides
- Fix “Attempt to Perform Arithmetic on Nil” in Roblox Luau
- Fix “X Is Not a Valid Member of Y” Errors in Roblox Studio
- Roblox RemoteEvent Not Firing? How to Fix OnServerEvent
- Fix “Infinite Yield Possible” WaitForChild Warning in Roblox
Sources
- Instance reference (FindFirstChild, WaitForChild) – Roblox Creator Hub
- Player reference (Character, CharacterAdded) – Roblox Creator Hub
- Players reference (LocalPlayer, GetPlayerFromCharacter) – Roblox Creator Hub
- WorldRoot reference (Raycast) – Roblox Creator Hub
- Model reference (PrimaryPart, GetPivot, PivotTo) – Roblox Creator Hub
- nil – Luau documentation, Roblox Creator Hub
- Debugging in Studio (breakpoints, Watch window) – Roblox Creator Hub
- “Attempt to index nil with ‘WaitForChild’” – Roblox DevForum
- “Attempt to index nil with ‘FindFirstChild’” on minigame script – Roblox DevForum
Frequently asked questions
What does "attempt to index nil with" mean in Roblox?
Your script used a dot, colon, or brackets on a value that is nil. The name in quotes is the property, child, or method you tried to reach, not the thing that is missing. The missing thing is whatever sits just before it on that line.
Why do I get "attempt to index nil with 'Humanoid'"?
Usually the character is nil because it hasn't spawned yet, or the part that touched something wasn't part of a character. Wait for the character with player.Character or player.CharacterAdded:Wait(), and check for nil before using it.
Why is LocalPlayer nil in my script?
Players.LocalPlayer is only set on the client. In a server Script it is nil, so any code like Players.LocalPlayer.Name fails there. Use a LocalScript, or get the player from an event such as PlayerAdded instead.
Should I use FindFirstChild or WaitForChild to fix it?
Use WaitForChild when the object will appear soon, such as parts of a character or things replicated from the server. Use FindFirstChild when the object may legitimately not exist, and then check the result for nil before you use it.