I personally don't know how a built in function can be indexed as "nil" butthis error appeared and it haulted my nextbot's movement. heres my code that is causing this
if (!self:GetPlayerVisible() and chasing_timer > chasing_time) then
self.stopchasing = true
self.enraged = false
print("Chase stopped")
print("Increasing escaped chases count, new:", self.escapedchases)
self.escapedchases = self.escapedchases + 1
end
I tried replacing the "!" with "not" but it did nothing.
I don't know what self is but that table does not contain an element GetPlayerVisible. Hece you may not call it. Calling nil values doesn't make sense.
Ask yourself why you think this function exists. Typical reasons are typos, indexing the wrong table or not having implemented a function yet.
To avoid this error you basically have two options. Make sure you call something that exists, or don't call it.
function teleportTo(placeCFrame)
local plyr = game.Players.LocalPlayer;
if plyr.Character then
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame;
end
end
teleportTo(game:GetService("Workspace").game:GetService("Workspace").Zeppelin.FuelTank1.Tank.CFrame)
my code is here, idk much about coding thanks for helping
and if you told me how to make the player teleport to a moving object it would be so super
The error is telling you what is going on.
When the code was being interpreted line by line, it expected the next symbol to be end, but instead it got =.
That means that something about how you're using the equals sign is incorrect. So when we look at the line :
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame
You cannot assign a value on the same line as a return command. return is used to pipe a value out of a function so it can be used where the function was called.
But I'm pretty sure that isn't what you intended, you just want to set a player's position. So to fix your issue, remove the return.
if plyr.Character then
plyr.Character.HumanoidRootPart.CFrame = placeCFrame
end
The reason you are getting an error is because = is usually defined as setting a value, and no code can be executed after a return or it will error
If you wanted to, you could add return after all the code is done executing
Ok so i will show an exemple
I want do this :
local x = {}
repeat
-- wait function
until x.lel.ciao
But i've this error :
input:3: attempt to index a nil value (field 'lel')
So i can just do this :
local x = {}
repeat
-- wait function
until x.lel and x.lel.ciao
but if i've a long path how can i do ?
like :
x.lel.ciao.value1.title1.text1
i dont want do :
local x = {}
repeat
-- wait function
until x.lel and x.lel.ciao and x.lel.ciao.value1 and x.lel.ciao.value1.title1 and x.lel.ciao.value1.title1.text1
Someone have an idea ? like a function safepath(x.lel.ciao.value1.title1.text1)
Just like Egor's comment (thanks Egor), debug.setmetatable allows you to set a metatable for object type (not object instance).
This comes with an issue,
All the objects of that type will also inherit the mtatable.
This means, you will experience issues that will make your code harder to debug, as it is definitely important to get this kind of feedback from nil values.
Take in example the following code:
debug.setmetatable(nil, { __index = {} })
repeat
. . . -- Your code goes here
until x.lel.ciao.value1.title1.text1
function getFrom(data, value)
return date[value]
end
. . . -- More code
From this simple scope perspective, you might quickly see the issue, but imagine this code being buried by thousands of lines and functions.
You will eventually run into insanity as it only returns nil, which shouldn't happen at all because, well, you are sure your data variable has such value, right?
In order to avoid such thing from happening, you should safely do it like this:
debug.setmetatable(nil, { __index = {} })
repeat
. . . -- Your code goes here
until x.lel.ciao.value1.title1.text1
debug.setmetatable(nil, nil)
According to the Lua reference, setting a metatable to nil will remove the metatable, this way you will only temporally ignore feedback from nil while running inside the repeat loop.
I'm trying to make a gmod gamemode. In my init.lua I wanted it so that way team members can't hurt each other. So I used this code
function GM:EntityTakeDamage( target, dmginfo )
if ( target:IsPlayer() and dmginfo:IsPlayer() ) then
if (dmginfo:Team() == target:Team()) then
dmginfo:ScaleDamage( 0.0 ) // Sets damage to 0
end
end
end
However it's giving me the error telling me that IsPlayer() is a nil value even though it should be returning a boolean. It points to no other lines other then the line with IsPlayer() and it's saying it is IsPlayer()
you have a typo in line 3. dminfo
You should narrow down which of your multiple IsPlayer() calls actually is nil
dmgInfo is a CTakeDamageInfo which has no function IsPlayer()
single line Lua comments are opened with --, not //
https://wiki.garrysmod.com/page/Category:CTakeDamageInfo
If you call a function and it says its nil, then check if it even exists. Or even better, check this befor you use the function in the first place.
And to prevent you from coming back in a minute, CTtakeDamageInfo also does not have a function Team() as well.
Check out CTDamageInfo:GetAttacker()
For example, I have a table
table.insert( t, 1, function()
print ("rock");
end );
Is there any way to get function name from this table. I know that I can store name like a key, but what if I want to keep numeric index and also I want to know function name?
Is there any way to do it?
Thanks, on advance.
Say you have this code:
t = {}
x = 5
table.insert(t, 1, x)
t would then be {[1] = 5}. "5" is just a number - it has no name, and isn't associated with the variable "x"; it's a value.
In Lua, functions are treated exactly the same way, as values:
t = {}
x = function() print("test! :D") end
table.insert(t, 1, x)
The value of x is not associated with x in any way, shape, or form. If you want to manually name a function, you can do it by wrapping the function in a table, for example:
t = {}
x = function() print("test! :D") end
table.insert(t, 1, {
name = "MyFunctionName",
func = x
})
That is how you would do it!
...unless..
..you break the rules!
When Lua was developed, the developers realised that the anonymous nature of functions would make productive error messages difficult to produce, if not impossible.
The best thing you'd see would be:
stdin: some error!
stdin: in function 'unknown'
stdin: in function 'unknown'
So, they made it so that when Lua code was parsed, it would record some debug information, to make life easier. To access this information from Lua itself, the debug library is provided.
Be very careful with functions in this library.
You should exert care when using this library. The functions provided here should be used exclusively for debugging and similar tasks, such as profiling. Please resist the temptation to use them as a usual programming tool: they can be very slow. Moreover, several of these functions violate some assumptions about Lua code (e.g., that variables local to a function cannot be accessed from outside or that userdata metatables cannot be changed by Lua code) and therefore can compromise otherwise secure code.
To achieve your desired effect, you must use the debug.getinfo function; an example:
x = function()
print("test!")
print(debug.getinfo(1, "n").name)
end
x() -- prints "test!" followed by "x"
Unfortunately, the form of debug.getinfo that operates directly on a function doesn't fill the name argument (debug.getinfo(x, "n").name == nil) and the version above requires you to run the function.
It seems hopeless!
...unless..
..you really break the rules.
The debug.sethook function allows you to interrupt running Lua code at certain events, and even change things while it's all happening. This, combined with coroutines, allows you to do some interestingly hacky stuff.
Here is an implementation of debug.getfuncname:
function debug.getfuncname(f)
--[[If name found, returns
name source line
If name not found, returns
nil source line
If error, returns
nil nil error
]]
if type(f) == "function" then
local info = debug.getinfo(f, "S")
if not info or not info.what then
return nil, nil, "Invalid function"
elseif info.what == "C" then
-- cannot be called on C functions, as they would execute!
return nil, nil, "C function"
end
--[[Deep magic, look away!]]
local co = coroutine.create(f)
local name, source, linedefined
debug.sethook(co, function(event, line)
local info = debug.getinfo(2, "Sn")
name = info.namewhat ~= "" and info.name or nil
source, linedefined = info.short_src, info.linedefined
coroutine.yield() -- prevent function from executing code
end, "c")
coroutine.resume(co)
return name, source, linedefined
end
return nil, nil, "Not a function"
end
Example usage:
function test()
print("If this prints, stuff went really wrong!")
end
print("Name = ", debug.getfuncname(test))
This function isn't very reliable - it works sometimes, and doesn't others. The debug library is very touchy, so it's to be expected.
Note that you should never use this for actual release code! Only for debugging!
The most extreme case that is still acceptable is logging errors on piece of released software, to help the developer fix issues. No vital code should depend on functions from the debug library.
Good luck!
The function hasn't got any name. If you want you can assign it to a named variable:
theFunction = t[1]
-- Call it:
theFunction()
If what you want is storing a named function to the table, define it beforehand and use its name to store it:
theFunction = function()
print ("rock");
end
table.insert(t, 1, theFunction)
If this is not what you meant, give more details; for example how you would like to access the function. You're question is a bit misty.
The thing is table.insert considers the table as a sequence, only with numeric keys.
If you want to be able to call the function as t.fun() you'll have to use the table as an associative array and hence use a string as key. (BTW any type except nil or NaN are allowed as key)
t={}
t['MyFun']=function print'foo' end
t.myFun() -- uses syntactic sugar for string keys that are valid identifiers.
You might also notice that functions are passed by reference. So all functions are actually anonymous, and are just stored as a value to a certain key or variable.
You can store the names in a separate table.
functions = {}
functionNames = {}
function addFunction(f, name)
table.insert(functions, f)
functionNames[name] = f
end
To get the function, you can use the index. Once you have the function, you can get its name from function_names:
f = functions[3]
name = functionNames[f]
Good luck!