• file.Exists problem (Newbie developer :/)
    3 replies, posted
Hi people! This is my first post on facepunch. I decided to make an account and make a post about my problem. I'm recently started making gmod addons (simple and test ones). Today I'm making the most complicated and complexed addon I ever made :/ But I have found a problem that i cant solve. ERROR: [CODE][ERROR] addons/rpcharacter/lua/rpcharacter/server/sv_functions.lua:7: bad key to string index (number expected, got string) 1. error - [C]:-1 2. __index - lua/includes/extensions/string.lua:310 3. hasCharacter - addons/rpcharacter/lua/rpcharacter/server/sv_functions.lua:7 4. unknown - addons/rpcharacter/lua/rpcharacter/server/sv_commands.lua:2 5. unknown - lua/includes/modules/concommand.lua:54[/CODE] sv_functions.lua [CODE]local meta = FindMetaTable( "Player" ) function meta:getCharacterFile() return "rpcharacter/characters/" .. self:UniqueID() .. ".txt" end function meta:hasCharacter() local file = self:getCharacterFile() return file.Exists( "data", "GAME" ) -- Line 7. ERROR IS HERE end[/CODE] sv_commands.lua [CODE]concommand.Add("chdebug", function(ply) print(ply:hasCharacter()) end)[/CODE] Also i tried to google it but I found nothing that could fix it. May someone help me and explain why this is not working? EDIT: added error line
Line 7 is trying to index the string you set on line 6 rather than the file library. Change your file variable's name. The rest of the code is wrong too. [code] function meta:hasCharacter() return file.Exists( self:getCharacterFile(), "DATA" ) -- Line 7. ERROR IS HERE end [/code]
Thanks that worked! I changed the function to: [CODE]function meta:hasCharacter() local path = self:getCharacterFile() return file.Exists( path, "DATA" ) end[/CODE] and it worked ;) I think it's better i do the variable like I do in expression2 of wiremod first letter in upper case
[QUOTE=Willox;47692506]Line 7 is trying to index the string you set on line 6 rather than the file library. Change your file variable's name. The rest of the code is wrong too. [code] function meta:hasCharacter() return file.Exists( self:getCharacterFile(), "DATA" ) -- Line 7. ERROR IS HERE end [/code][/QUOTE] Wow you answered too quickly, it's hard to write using iPad keyboard... [editline]9th May 2015[/editline] [QUOTE=SmOkEwOw;47692536]Thanks that worked! I changed the function to: [CODE]function meta:hasCharacter() local path = self:getCharacterFile() return file.Exists( path, "DATA" ) end[/CODE] and it worked ;) I think it's better i do the variable like I do in expression2 of wiremod first letter in upper case[/QUOTE] Oh and actually in GMod lua conventions you should write first letter of methods uppercase. Like done on lib functions.
Sorry, you need to Log In to post a reply to this thread.