• How to use File.write/ file.read?
    13 replies, posted
Hello. excuse me for any grammatical errors. I want edit some options of my script by the game chat, using some command like "!command". I want do the edit it permanently so I have to use the data file. I'm doing it, but i think I wrong please help me: [CODE] function SetConfig( pl, text, teamonly ) ptext = string.Explode( " ", string.lower(text)) if ((ptext[1] == "!config") or (ptext[1] == "/config")) then if (ptext[2] == "option1") then file.Write( "option1.txt", ptext[3] ) end end hook.Add( "PlayerSay", "Chat", SetConfig ) [/CODE] It is correct?
I'm not sure what your goal is but the code above will do the following. If the player says "!config option1" then it will completely wipe everything in the file garrysmod/data/option1.txt and replace it with whatever is immediately after "!config option1".
[QUOTE=YourStalker;49510326]I'm not sure what your goal is but the code above will do the following. If the player says "!config option1" then it will completely wipe everything in the file garrysmod/data/option1.txt and replace it with whatever is immediately after "!config option1".[/QUOTE] I would like it works like you said, but, if I type in the chatbox !config option1 10, nothing happens, in data folder, there isn't any .txt file. Why?
[QUOTE=Predda;49509711] [CODE] function SetConfig( pl, text, teamonly ) ptext = string.Explode( " ", string.lower(text)) if ((ptext[1] == "!config") or (ptext[1] == "/config")) then if (ptext[2] == "option1") then file.Write( "option1.txt", ptext[3] ) end end hook.Add( "PlayerSay", "Chat", SetConfig ) [/CODE][/QUOTE] Is that your [B]exact[/B] code as it is in your Lua file? If so, you're missing an "[B]end[/B]": [code] function SetConfig( pl, text, teamonly ) ptext = string.Explode( " ", string.lower(text)) if ((ptext[1] == "!config") or (ptext[1] == "/config")) then if (ptext[2] == "option1") then file.Write( "option1.txt", ptext[3] ) end end end hook.Add( "PlayerSay", "Chat", SetConfig ) [/code] I imagine nothing is happening because it's erroring out when it gets loaded, but I don't have a whole lot of information to go on.
[QUOTE=Mista Tea;49513242]Is that your [B]exact[/B] code as it is in your Lua file? If so, you're missing an "[B]end[/B]": [code] function SetConfig( pl, text, teamonly ) ptext = string.Explode( " ", string.lower(text)) if ((ptext[1] == "!config") or (ptext[1] == "/config")) then if (ptext[2] == "option1") then file.Write( "option1.txt", ptext[3] ) end end end hook.Add( "PlayerSay", "Chat", SetConfig ) [/code] I imagine nothing is happening because it's erroring out when it gets loaded, but I don't have a whole lot of information to go on.[/QUOTE] I already solved it in private, but this still not works! [editline]12th January 2016[/editline] I typed it in lua/autorun/client It is wrong?
yeah, this is serverside - check the wiki if you're ever unsure as it will tell you if a hook is server or client side
[QUOTE=NiandraLades;49513275]yeah, this is serverside - check the wiki if you're ever unsure as it will tell you if a hook is server or client side[/QUOTE] Awesome, thank you! one question, in the wiki/hooksection, where it says, if is a server/client side?
[QUOTE=Predda;49513325]Awesome, thank you! one question, in the wiki/hooksection, where it says, if is a server/client side?[/QUOTE] [img]https://i.gyazo.com/6a5634a1974fcf768d2f8497316275e7.png[/img] [B]Blue [/B]= serverside [B]Orange [/B]= clientside [B]Both [/B]= shared
[QUOTE=Mista Tea;49513404][img]https://i.gyazo.com/6a5634a1974fcf768d2f8497316275e7.png[/img] [B]Blue [/B]= serverside [B]Orange [/B]= clientside [B]Both [/B]= shared[/QUOTE] don't ban me for this post.. i honestly always though it was yellow :O
If you want to keep it totally clientside try using OnPlayerChat instead. This all depends on whether you want the data to save to he client or the data to save to the server.
[QUOTE=Mista Tea;49513404][img]https://i.gyazo.com/6a5634a1974fcf768d2f8497316275e7.png[/img] [B]Blue [/B]= serverside [B]Orange [/B]= clientside [B]Both [/B]= shared[/QUOTE] Thank You!!!
[QUOTE=Mista Tea;49513404][img]https://i.gyazo.com/6a5634a1974fcf768d2f8497316275e7.png[/img] [B]Blue [/B]= serverside [B]Orange [/B]= clientside [B]Both [/B]= shared[/QUOTE] It bugs me how there's no reference at all, [I]not even a label on mouse hover[/I] (I found it out by checking its source code). Doesn't help either that it goes against the colors used by gmod itself ( blue icon - client, orange icon - server). Maybe sticking a CL / SV / SH inside that box could be useful.
[QUOTE=Coment;49543604]It bugs me how there's no reference at all, [I]not even a label on mouse hover[/I] (I found it out by checking its source code). Doesn't help either that it goes against the colors used by gmod itself ( blue icon - client, orange icon - server). Maybe sticking a CL / SV / SH inside that box could be useful.[/QUOTE] The bottom of the page shows what type of hook it is as well
[QUOTE=Coment;49543604]It bugs me how there's no reference at all, [I]not even a label on mouse hover[/I] (I found it out by checking its source code). Doesn't help either that it [b]goes against the colors used by gmod itself[/b] ( blue icon - client, orange icon - server). Maybe sticking a CL / SV / SH inside that box could be useful.[/QUOTE] Um... what? The wiki uses blue for server, orange for client, and green for menu, just like the print message colors in gmod. The old wiki used to have Server/Client/Shared/Menu in the labels, but Garry decided this was better.
Sorry, you need to Log In to post a reply to this thread.