bad argument #N to '<function>'
A script passed the wrong kind of value to the game, almost always because something it needed (a vehicle, a model) never loaded.
Do this first
Find out whether the model or vehicle it is trying to use actually exists on your server.
What it means
A native or standard function got the wrong type in that argument slot. The message says which slot and what it wanted:
bad argument #1 to 'TaskGoToCoordAnyMeans' (number expected, got nil)
Argument numbering starts at 1. For a method called with a colon, argument #1 is the object itself, so #2 is the first thing inside the parentheses.
What causes it
Ordered by how often it is the answer.
- 1
An entity handle that is 0 or nil
CreateVehiclereturned 0 because the model never loaded, and the handle went straight into another native. By far the most common form. - 2
A model name that was never hashed
Most model slots want a hash, not a string. Use
GetHashKey(name)or the backtick literal. - 3
A vector where three numbers are wanted, or the reverse
SetEntityCoords(ped, coords)fails whereSetEntityCoords(ped, coords.x, coords.y, coords.z, ...)is expected. - 4
A nil from a config or a database row
Reading a missing key produces nil silently; the error surfaces one line later, here.
- 5
Wrong argument order
Two numbers swapped will not error. A number where a string belongs will.
How to tell which one is yours
Print every argument with its type on the line above:
print(('ped=%s type=%s'):format(tostring(ped), type(ped)))If the value is 0 rather than nil, the real bug is upstream: something returned a failed handle and nobody checked it.
Where to look
The named line, then the line that PRODUCED the bad argument.
How to fix it
Load models before spawning, and check handles before using them:
local model = GetHashKey('adder')
RequestModel(model)
local deadline = GetGameTimer() + 5000
while not HasModelLoaded(model) and GetGameTimer() < deadline do Wait(0) end
if not HasModelLoaded(model) then return end
local veh = CreateVehicle(model, coords.x, coords.y, coords.z, heading, true, false)
if not DoesEntityExist(veh) then return end
SetModelAsNoLongerNeeded(model)Still stuck on your own code?
This page is what this error usually means. Paste your console into Server Fixer and it will rank everything in it and tell you what to do first, and if you would rather not touch it, ORBIE can repair the script and hand the resource back ready to drop in.
bug_reportOpen Server Fixer