General > Developers Corner

Timer issue

<< < (2/3) > >>

Digital Spit:
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: ---if SERVER then
-- code --
end
--- End code ---

Stickly Man!:
No problem!


--- Quote from: Digital Spit on June 26, 2014, 12:45:06 PM ---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

--- End quote ---

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.)


--- Quote from: Digital Spit on June 26, 2014, 12:45:06 PM ---Also if I wanted to make the server run this on startup would I just include


--- Code: ---if SERVER then
-- code --
end
--- End code ---

--- End quote ---

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.

Cobalt:

--- Quote from: Stickly Man! on June 26, 2014, 02:35:57 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.

--- End quote ---
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

Digital Spit:
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: ---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


--- End code ---

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.

An Error Has Occurred!

array_keys(): Argument #1 ($array) must be of type array, null given

[0] Board index

Go to full version