2

Before I explain my problem, I have already had a look around stack overflow and looked through countless questions regarding this error message.

I have tasked myself with building an updating stats page on my website which updates itself with realtime stats from my Garry's Mod game server. I have written a lua script (which I will include below) that in theory should pull the current map name and number of players connected to seperate text files.

Here is my program in it's current state (I have not added a loop for updating just yet, trying to get working in simplest form first):

// Custom script to pull current server statistics and return to website
local current_map = game.GetMap()
local current_players = #player.GetAll()
map = io.open("../../../../map.txt", "w")
map.write(current_map)
map.close()
players = io.open("../../../../players.txt", "w")
players.write(current_players)
players.close()

When I start up the server, I get an error in my console with Line 4: attempt to index global 'io' (a nil value)

After trying countless numbers of things and editing the code many times, I am still unable to get this working when it should in theory be so simple, and I have no idea what is wrong.

pppery
  • 3,731
  • 22
  • 33
  • 46
pythagon
  • 105
  • 2
  • 10
  • 1
    That looks like gmod doesn't include the `io` module in it. Do you know that it does? Does gmod have its own `io`-type module instead? Does it even allow things to do `io`-type stuff (reading to and writing from disk directly)? – Etan Reisner Oct 12 '15 at 17:44
  • @EtanReisner Wow, that never occured to me that Garry's mod may not include the module. Here is a picture of the server's module folder- I can't see anything that may relate to IO, unless you can tell me otherwise? http://i.imgur.com/6Hr8EWO.png – pythagon Oct 12 '15 at 17:48
  • The `io` module is a lua built-in module but the hosting environment doesn't need to load it if they don't want to. You won't see any files related to it. You'd have to check the gmod documentation. – Etan Reisner Oct 12 '15 at 17:49
  • @pythagon For things like that, reference the relevant documentation. – Cubic Oct 12 '15 at 17:49
  • It would make perfect sense for gmod to **not** include the `io` module. You don't want "random" scripts being able to read and write all over your hard-drive for example. – Etan Reisner Oct 12 '15 at 17:50
  • @EtanReisner Cheers. Did a quick search through the online documentation/scripting wiki, and it didn't return any results for `io`. Therefore, is there a way in which I can include the io module in just my script, as I understand about what you said with scripts not being able to read anything on the hard drive. – pythagon Oct 12 '15 at 17:54
  • You can try `require("io")` but it almost certainly won't work either (or what would be the point of not loading it by default; unless there is configuration that controls which scripts can use it... in which case that could just as easily load it for those scripts to begin with). I have to imagine someone has does something vaguely like this before (or at least that there is documentation on ways to communicate from or to a gmod script out there). – Etan Reisner Oct 12 '15 at 17:58
  • @EtanReisner That code didn't work, however after a little more trawling around the scripting wiki (that I probably should have spent 15 mins doing before posting this...), I found that Garry's Mod does indeed have it's own, (very) simple file IO library. It's a shame I didn't find this earlier, however thank you very much for your help! – pythagon Oct 12 '15 at 18:18
  • Yea, Garry's Mod replaces a lot of the Lua standard library, sometimes for no reason. – Colonel Thirty Two Oct 12 '15 at 21:41

1 Answers1

2

Anyone in the future that has this problem;

After some trawling around the scripting wiki, I found that Garry's Mod has it's own file IO library.

A link to this page on the wiki can be obtained below:

http://wiki.garrysmod.com/page/file/Write

pythagon
  • 105
  • 2
  • 10