• Init.lua(101) attempted to call a nill value?
    11 replies, posted
Im getting this problem EVERY time i try to call this function function RunFaster( ply ) ply:SetNWInt("SkillPoints", ply:GetNWInt("SkillPoints") - 1) "You Now Have 1+ Sprint Speed " ply:SetRunSpeed( 800 ) end
That isn't valid Lua, which line (of the snippet you posted) is causing the error?
please use [LUA] tags next time and [LUA]function RunFaster( ply ) ply:SetNWInt("SkillPoints", ply:GetNWInt("SkillPoints") - 1) "You Now Have 1+ Sprint Speed " <--- WRONG ply:SetRunSpeed( 800 ) end [/LUA] should be: [LUA]function RunFaster( ply ) ply:SetNWInt("SkillPoints", ply:GetNWInt("SkillPoints") - 1) ply:ChatPrint("You Now Have 1+ Sprint Speed ") // or any other function. ply:SetRunSpeed( 800 ) end [/LUA]
-snip Ninja'd
[QUOTE=sweeneypaul;27566072]-snip[/QUOTE] -snap- ninja'd too.
I was just showing why he got the error not how to do it how he wants :P
[QUOTE=sweeneypaul;27566115]I was just showing why he got the error not how to do it how he wants :P[/QUOTE] That isn't what caused the error anyway, the error is obviously caused by a call to a nil value. The line you were talking about doesn't contain any calls. [editline]21st January 2011[/editline] [QUOTE=blown25;27566043]please use [LUA] tags next time[/QUOTE] [noparse][lua]code[/lua][/noparse]
[QUOTE=thejjokerr;27566471]Also do: [lua]ply:SetNWInt("SkillPoints", ply:GetNWInt("SkillPoints", 0) - 1)[/lua] Setting a default value in GetNWInt is smart if you're not sure if it has already been sent/set.[/QUOTE]
it works now and i dont have to set the default value to 0 i alredy created CFG_SkillPoints = 0 before that function PD:Now how i prevent the player from buying the new ability if hes skillpoints arent 1? becouse if i have 0 skillpoints and then use the function my skillpoints become -1
Ue an if statement to check if the player has more than 0 skillpoints, something like this: [lua]function RunFaster(ply) local sp = ply:GetNWInt("SkillPoints", 0) if sp > 0 then ply:SetNWInt("SkillPoints", sp - 1) ply:ChatPrint("You Now Have 1+ Sprint Speed") ply:SetRunSpeed(800) end ply:ChatPrint("You don't have enough Skill Points") end[/lua]
The reason it errored as "tried to call a nil value" is because lua can pass a string as function parameter without parantesis, thus lua expected the call before the string (ply:SetNWInt) to return a function. But as you'd expect is returns nil. Just thought I'd clear up any likely confusion about this:smile: [url=http://codepad.org/l79xvuhu]Example of a similiar non erroring code snippet showing what I just said[/url]
Sorry, you need to Log In to post a reply to this thread.