Reload script?

Hi, im trying to create a script that will reload/restart/whatever itself each 30 seconds. Im thinking I could make a timer, then restart when the time is 0.


Like, timer.Simple( 5, function() restart() end )

[but restart isn’t a function?]

Why would you need to re-load your script every 30 seconds?

so I could update information in a file.

  1. Wouldn’t you need to reload the file instead of the script, or is the file the script?
  2. Why would you update it while the server is live by editing lua files?
  3. What are you trying to do?

I suppose you could try some kind of hacky workaround like doing lua_run for your serverside scripts and lua_run_cl for the clientside ones (after reading them)

Not sure if that would work though

I want to reload the script, so it would edit a file with new content.
what
And I’m trying to get the names of players connected to the server, and put that in a file that Bash (I run my server on Debian) and can be used for any reason. Sense I can’t seem to find a way to make the file update when a player joins, I’m going to make it simpler by just reloading the script every 30 seconds, which in turn will re-write the file in case a player has joined in the last 30 seconds.

Use the PlayerConnect hook?



hook.Add( 'PlayerConnect', 'WriteNamesToFile', function( name, ip )
    file.Append( 'somefile.txt', name .. ' joined the server at ' .. util.DateStamp() )
end


Right now I just need the name of the function that will reload the script. I barely started the whole GLua thing.

There isn’t a function that reloads scripts, as far as I know, but as I was saying before, you could try running your code with the lua_run and lua_run_cl commands, so then when it would be run, and probably replace your old code. But, if you used the PlayerConnect hook, you wouldn’t need to do something as hacky as that.

Alright, ill try my way with the hook.

You can use timer.Simple with the include function…

Yes but I need a function that will reload the script, for an infinite loop.

what I suggested will do exactly what you want. If you use include, the file will be loaded again and the timer set again, and the same will happen after the timer runs. infinitely.

[editline]5th February 2016[/editline]

or you can make a function and have timer.Create call it each X seconds with the repetitions set to 0 (so it’ll never stop)

I

I just have no idea how to use timers. FML

I have:
timer.Create( “TimerTest” , 10 , 0 , print(“what”))
Then i get:
[ERROR] lua/autorun/luatest.lua:9: bad argument #4 to ‘Create’ (function expected, got no value)

  1. Create - [C]:-1
  2. unknown - lua/autorun/luatest.lua:9

That’s because the 4th value has to be a function:
[lua]
timer.Create( “TimerTest” , 10 , 0 , function() print(“what”) end)
[/lua]

EDIT: All hail FP’s code highlighter and its great job.

I tried that, nothing happened.




if SERVER then
timer.Create( "TimerTest" , 10 , 0 , function() Msg("what") end)
end


It takes 10 seconds to run after the file is loaded. Where did you place the file?

Autorun. I waited more than 10 seconds, too.

garrysmod\lua\autorun\Luatest.lua

Are you sure the file is loading?
Try running “lua_openscript Luatest.lua” on the server console

Did that, it just said “Running script autorun\Luatest.lua…”
I also moved it outside autorun, still no results.