• Print

Author Topic: Timer issue  (Read 6973 times)

0 Members and 1 Guest are viewing this topic.

Offline Digital Spit

  • Jr. Member
  • **
  • Posts: 79
  • Karma: 1
Timer issue
« on: June 26, 2014, 02:38:23 am »
I'm working on a little script that'll change the job of a player after a given time, but for some reason I can't get the timer to work, what am I doing wrong? I've tried moving it all to one line and I've also tried rewriting the timer, but alas no result :/

Code: [Select]
local Players = player.GetAll()
for i = 1, table.Count(Players) do
local ply = Players[i]
if (ply:Team() == TEAM_DEFAULTED ) then
timer.Create("randomJob", 180, 1,
ply:changeTeam( job_table[randomJob], true),
ply:PrintMessage(HUD_PRINTCENTER, "Your job has been auto-selected")
end)
end

Offline Cobalt

  • Full Member
  • ***
  • Posts: 216
  • Karma: 44
  • http://steamcommunity.com/id/__yvl/
Re: Timer issue
« Reply #1 on: June 26, 2014, 05:49:30 am »
Timers have unique ids like hooks. If two are the same they will overwrite.

Offline Digital Spit

  • Jr. Member
  • **
  • Posts: 79
  • Karma: 1
Re: Timer issue
« Reply #2 on: June 26, 2014, 11:28:33 am »
I've changed the ID of the timer, but that give the same error...any other suggestions?  :)

Code: [Select]
[ERROR] lua/money.lua:55: ')' expected (to close '(' at line 52) near 'end'
  1. unknown - lua/money.lua:0

That's the error I keep getting and I'm not sure why. All of the parenthesis close out perfectly, what am I missing?

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Timer issue
« Reply #3 on: June 26, 2014, 11:45:58 am »
Two problems-

Firstly, the code executed in your timer needs to be in a function like so:
Code: Lua
  1. local Players = player.GetAll()
  2. for i = 1, table.Count(Players) do
  3.         local ply = Players[i]
  4.         if (ply:Team() == TEAM_DEFAULTED ) then
  5.                 timer.Create("randomJob", 180, 1, function()
  6.                         ply:changeTeam( job_table[randomJob], true),
  7.                         ply:PrintMessage(HUD_PRINTCENTER, "Your job has been auto-selected")
  8.                 end)
  9.         end
  10. end
Basically, you missed the function() (line 5), and then just need to match up the 'end's (line 9).


Next, as Cobalt mentioned, you are creating a timer for every player, but you're setting the ID of the timer to "randomJob"- this means you will only be able to have one working timer on the server at a time. You can fix that by appending some player-specific tag to the timer ID, like so:
Code: Lua
  1. timer.Create("randomJob" .. ply:UserID(), 180, 1, function()
This will append the players User ID to the timer name, thus being unique for each player, but never creating a duplicate for the same player.


Thirdly, for readability, you could condense your first three lines into:
Code: Lua
  1. for _, ply in pairs( player.GetAll() ) do
.. but that's mainly personal preference. Your way may be faster performance-wise, but with such small table sizes the difference in performance is negligible compared to having less code to read through.

Hope that helps!
« Last Edit: June 26, 2014, 11:54:00 am by Stickly Man! »
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Digital Spit

  • Jr. Member
  • **
  • Posts: 79
  • Karma: 1
Re: Timer issue
« Reply #4 on: June 26, 2014, 11:53:49 am »
Thank you very much Cobalt and Sitckly Man! - It's working now! You guys rock

Offline Digital Spit

  • Jr. Member
  • **
  • Posts: 79
  • Karma: 1
Re: Timer issue
« Reply #5 on: June 26, 2014, 12:45:06 pm »
Ah, Stickly I was actually about to post a question asking which would be better to use, a for loop that goes through the player table or if there was another way, I see you've already done that for me though, again thank you!

Also if I wanted to make the server run this on startup would I just include

Code: [Select]
if SERVER then
-- code --
end

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Timer issue
« Reply #6 on: June 26, 2014, 02:35:57 pm »
No problem!

I was actually about to post a question asking which would be better to use, a for loop that goes through the player table or if there was another way

There is lots of discussion on whether to make your own for loop, or use pairs or ipairs. Based on my reading (need to sit down and actually test this someday) I think pairs is the best to use for most loops of a table since it's simple to use (less code) and is fast enough. ipairs is slower, and should only be used if you specifically need to ignore the non-integer key-value pairs in a table, or some other special use cases involving arrays with "holes" in them. Lastly, doing your own for i loop is the fastest, but really is only needed if you're dealing with really large tables (greater than 5000 items, maybe), or if it's being called frequently (every frame/tick/think, etc., but hopefully that should never have to happen.)

Also if I wanted to make the server run this on startup would I just include

Code: [Select]
if SERVER then
-- code --
end

If you want it running on startup, then just put it in your server's autorun lua folder. (/gmod/lua/autorun/, or something like that). If you're not doing AddCSLuaFile anywhere in your code, then the clients should never get this file, thus the check for server is not needed. However, if you want to ensure the script only runs on the server, it doesn't hurt to put your code in the if SERVER then block.
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Cobalt

  • Full Member
  • ***
  • Posts: 216
  • Karma: 44
  • http://steamcommunity.com/id/__yvl/
Re: Timer issue
« Reply #7 on: June 26, 2014, 05:34:45 pm »
There is lots of discussion on whether to make your own for loop, or use pairs or ipairs. Based on my reading (need to sit down and actually test this someday) I think pairs is the best to use for most loops of a table since it's simple to use (less code) and is fast enough.
next is the best one to use, in my opinion.

Edit: I tested the speeds of the 3 functions.


Test for yourself: http://puu.sh/9LLxp/8f63267a27.lua
« Last Edit: June 26, 2014, 06:29:09 pm by Cobalt »

Offline Digital Spit

  • Jr. Member
  • **
  • Posts: 79
  • Karma: 1
Re: Timer issue
« Reply #8 on: June 27, 2014, 11:12:36 am »
I've seem to run into another issue with this script, it's not loading when the server starts up :/

Here is what I've put into lua/autorun/server

Code: [Select]
job_table = {TEAM_CITIZEN, TEAM_HOBO, TEAM_BUILDER, TEAM_KINGHOBO, TEAM_BANKER, TEAM_MEDIC, TEAM_SECURITYGUARD, TEAM_BODYGUARD} // Job labels, for the job script
local jobCount = table.Count(job_table)
local randomJob = math.random(1, jobCount)
Msg("Random Job loaded successfully.")

for _, ply in pairs( player.GetAll() ) do
if (ply:Team() == TEAM_DEFAULTED ) then
timer.Create("randomJob" ..ply:UserID(), 120, 1, function()
ply:changeTeam( job_table[randomJob], true)
ply:PrintMessage(HUD_PRINTCENTER, "Your job has been auto-selected")
end)
end
end


Yet after 2 minutes if the player is still the under the team "TEAM_DEFAULTED" they are not switched to a random job

Any suggestions? I'm completely lost as to why it's not automatically working.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Timer issue
« Reply #9 on: June 27, 2014, 11:25:02 am »
I've seem to run into another issue with this script, it's not loading when the server starts up :/

Here is what I've put into lua/autorun/server

Code: [Select]
job_table = {TEAM_CITIZEN, TEAM_HOBO, TEAM_BUILDER, TEAM_KINGHOBO, TEAM_BANKER, TEAM_MEDIC, TEAM_SECURITYGUARD, TEAM_BODYGUARD} // Job labels, for the job script
local jobCount = table.Count(job_table)
local randomJob = math.random(1, jobCount)
Msg("Random Job loaded successfully.")

for _, ply in pairs( player.GetAll() ) do
if (ply:Team() == TEAM_DEFAULTED ) then
timer.Create("randomJob" ..ply:UserID(), 120, 1, function()
ply:changeTeam( job_table[randomJob], true)
ply:PrintMessage(HUD_PRINTCENTER, "Your job has been auto-selected")
end)
end
end


Yet after 2 minutes if the player is still the under the team "TEAM_DEFAULTED" they are not switched to a random job

Any suggestions? I'm completely lost as to why it's not automatically working.

Put it in a function, and hook it to something like PlayerInitialSpawn.
That way you could probably drop the whole loop, unless I've misunderstood what you're using it for, because then it would run for the player that joins. Instead of going through everyone all the time. It should look something like this, in that case:
Code: Lua
  1. function SelectRandomJob( ply )
  2.         if (ply:Team() == TEAM_DEFAULTED ) then
  3.                 timer.Create("randomJob" ..ply:UserID(), 120, 1, function()
  4.                         ply:changeTeam( job_table[randomJob], true)
  5.                         ply:PrintMessage(HUD_PRINTCENTER, "Your job has been auto-selected")
  6.                 end)
  7.         end
  8. end
  9. hook.Add( "PlayerInitialSpawn", "SelectRandomJob", SelectRandomJob )
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Digital Spit

  • Jr. Member
  • **
  • Posts: 79
  • Karma: 1
Re: Timer issue
« Reply #10 on: June 27, 2014, 01:24:31 pm »
Put it in a function, and hook it to something like PlayerInitialSpawn.
That way you could probably drop the whole loop, unless I've misunderstood what you're using it for, because then it would run for the player that joins. Instead of going through everyone all the time. It should look something like this, in that case:
Code: Lua
  1. function SelectRandomJob( ply )
  2.         if (ply:Team() == TEAM_DEFAULTED ) then
  3.                 timer.Create("randomJob" ..ply:UserID(), 120, 1, function()
  4.                         ply:changeTeam( job_table[randomJob], true)
  5.                         ply:PrintMessage(HUD_PRINTCENTER, "Your job has been auto-selected")
  6.                 end)
  7.         end
  8. end
  9. hook.Add( "PlayerInitialSpawn", "SelectRandomJob", SelectRandomJob )

That's exactly what I was trying to do, was just unaware of how to do it! Thank you

  • Print