Error reference
Roblox: “attempt to index nil with …”
This is the most common runtime error in Luau, and the message points at the wrong half of the problem. Read it backwards and it becomes obvious — then there are exactly six reasons it happens.
What it actually says: in attempt to index nil with 'Humanoid', Humanoid is what you asked for. The thing that is nil is whatever stood to the left of the dot. Nothing is wrong with Humanoid; something before it does not exist.
1. The instance has not replicated yet
The classic. A LocalScript runs the moment it is loaded, which can be before the object it needs has arrived from the server.
-- breaks intermittently
local gui = playerGui.MainGui.Frame
-- waits for it instead of assuming
local mainGui = playerGui:WaitForChild("MainGui", 10)
if not mainGui then
warn("MainGui never arrived — check that the server actually creates it")
return
end
local frame = mainGui:WaitForChild("Frame", 10)
Always pass a timeout. Without one, WaitForChild yields forever and you trade a clear error for a script that silently stops — see infinite yield possible.
2. player.Character does not exist yet
A Player object exists before its character model does. Reading player.Character.Humanoid on join is a race you usually lose.
-- breaks on join — attempt to index nil with 'Humanoid'
-- player.Character.Humanoid.WalkSpeed = 32
-- waits for whichever comes first: an existing character, or the next one
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- and handle respawns, because the old character is destroyed
player.CharacterAdded:Connect(function(newCharacter)
local newHumanoid = newCharacter:WaitForChild("Humanoid")
end)
The respawn half matters more than people expect: a reference captured once points at a character that no longer exists after the first death.
3. The name or the path is wrong
Instance lookup is case-sensitive and exact. workspace.SpawnPoint does not find an object named Spawnpoint, and a script that assumes a flat hierarchy breaks the moment someone groups things into a folder. This is the most common cause in AI-generated code, because the model guessed a plausible layout instead of reading yours.
4. A function returned nil and you kept going
FindFirstChild returns nil when there is no match — that is its job. So does a table lookup with a key that is not there, and any function whose early return path you forgot about.
-- FindFirstChild is allowed to return nil; act like it
local tool = backpack:FindFirstChild("Sword")
if not tool then return end
tool.Handle.Transparency = 0
-- guard the config lookup too
local config = ItemConfig[itemId]
if not config then
warn(("unknown item %q"):format(tostring(itemId)))
return
end
5. The instance was destroyed
After :Destroy() the variable still holds a reference, but the object is parented to nil and its children are gone. Anything that survives the destruction — a loop, a connection, a task.delay — will index it and fail. Disconnect connections and check instance.Parent before touching an object that another code path can remove.
6. You are on the wrong side of the client-server boundary
ServerStorage and ServerScriptService do not replicate to clients, so a LocalScript reaching into them gets nil. The same applies in reverse to things that exist only on one client. If a value must cross, it crosses through a remote — and then it must be validated on arrival, which is a separate problem covered in the RemoteEvent security checklist.
How to find yours in under a minute
- Read the line in the error and identify the token immediately before the reported member. That is your nil.
- Print it: print(playerGui:FindFirstChild("MainGui")). Nil means it is missing or misnamed; an instance means the problem is further down the chain.
- Check the Explorer while the game is running, not while it is stopped — the runtime hierarchy is what matters.
- If it only fails sometimes, treat it as a race and fix the timing, not the symptom.
Why AI-written code hits this so often: a model can produce syntactically perfect Luau while guessing your instance names, your folder structure and the order things load in. Those guesses only fail at runtime, which is exactly what a code review cannot catch — the runtime failure guide works through four of these in detail.
Let the agent hit the error instead of you
Baseplate runs your AI agent against a live Roblox Studio session: it playtests, reads the real stack trace, fixes the cause and re-tests until the console is clean. Free plan, your own AI account.
Download Baseplate free