This is a really dumb question but I can't find out the solution.
Here is the timer
[CODE]timer.Create( "ZombieSpawn", ZSpawnSpeed, 0, function()[/CODE]
Here is the code for the timer delay
[CODE]
function GM:Think()
if game.SinglePlayer() == 1 then
ZSpawnSpeed = 0.1
elseif game.MaxPlayers() == 2 then
ZSpawnSpeed = 4
elseif game.MaxPlayers() == 4 then
ZSpawnSpeed = 4
elseif game.MaxPlayers() == 8 then
ZSpawnSpeed = 3
elseif game.MaxPlayers() == 12 then
ZSpawnSpeed = 2
elseif game.MaxPlayers() == 16 then
ZSpawnSpeed = 2
elseif game.MaxPlayers() == 32 then
ZSpawnSpeed = 1
end
end[/CODE]
So here are the 2 problems. When the game loads it can't find the variable. When I refresh the gamemode without reloading the entire Garry's Mod, it will use the timer like it is supposed to. Any help would be awesome!
PS: I already tried other functions other then GM:Think but this one seems to work the best.
First of all - learn how to use tables.
Second of all - learn how scopes work.
Finally - show the whole code - are we supposed to guess where is what?
First of all - I know how to use tables I don't care about efficiency at the moment
Second of all - I've never heard of a scope I've taught myself how to program because people teach it in the stupidest ways possible
Finally - The only thing that's not showing is the function which you don't even need to know because that has nothing to do with the timer at all.
[QUOTE=Micro230;50427272]First of all - I know how to use tables I don't care about efficiency at the moment
Second of all - I've never heard of a scope I've taught myself how to program because people teach it in the stupidest ways possible
Finally - The only thing that's not showing is the function which you don't even need to know because that has nothing to do with the timer at all.[/QUOTE]
I'm guessing the function holds all your extra salt, and you don't want anyone to take any of it?
Seriously, we can't make much of what you've given us.
Also, you should have listened correctly to those people, any coding language I've ever heard of, and any guide ive read, discusses scope rather easily.
Scope is really liek a real scope
In coding, if you make a value or anything in that function that is new, say you make "local imsosalty = "I must take more salt" " in a function, you cant access it in another. unless you creat that variable outside the function.
Every time you use if, function, timer, ect anything within it till its end, is known as its scope. The first scope you encounter is your script, everything in your script is in the script scope.
A scope can also have multipel scopes withing multiple other scopes, its goes on.
[LUA]
//SCOPE BEGIN(0)
local toosaltyforjesus = "Im salty enough for the devil though"
function OurSetPlayer(ply)
//SCOPE BEGIN(1)
local Pepperisinferior = "The saltier the better"
OurPlayer = ply
print("Set player to " .. ply:Nick())
//SCOPE end(1)
end
if SERVER then
//SCOPE BEGIN(2)
print(toosaltyforjesus) // This orks because our variable is created outside this function, but not in any other function or check, or their scope.
print(Pepperisinferior) // this wont work because this was created in another function.
// hook.Add("PlayerInitialSpawn", "OurSetPlayer", OurSetPlayer)
hook.Add("PlayerSpawnedVehicle","OurSpawnedVehicle", OurSpawnedVehicle)
// hook.Add("FinishMove","OurFinishedMove", OurFinishedMove)
//SCOPE END(2)
end
//SCOPE END(0)
[/LUA]
Hopefully that helps. Its late and I cant teach well, but scope is quite simple.
(Am i doing something wrong or does the code tags not correctly display comments?)
[QUOTE=Micro230;50427272]First of all - I know how to use tables I don't care about efficiency at the moment
Second of all - I've never heard of a scope I've taught myself how to program because people teach it in the stupidest ways possible
Finally - The only thing that's not showing is the function which you don't even need to know because that has nothing to do with the timer at all.[/QUOTE]
Look here asshole, you came here for help, so don't give us that tone you fucking toenail
[QUOTE=Micro230;50427272]First of all - I know how to use tables I don't care about efficiency at the moment
Second of all - I've never heard of a scope I've taught myself how to program because people teach it in the stupidest ways possible
Finally - The only thing that's not showing is the function which you don't even need to know because that has nothing to do with the timer at all.[/QUOTE]
Efficiency doesn't matter, readability does. Learn when you should use tables. Also, the scope of a variable is when and where it's available, Lua uses lexical scoping.
Back to your code, why are you using global variables? And why are you running those operations in a think hook? Also game.SingePlayer returns a boolean not a number.
The easy solution to this would be to use tables.
[code]
local players_to_speed = {
[2] = 4,
[4] = 4,
[8] = 3,
[12] = 2,
[16] = 2,
[32] = 1
}
local speed = game.SinglePlayer() and 0.1 or players_to_speed[game.MaxPlayers()]
timer.Create("Shieet", speed, 0, function() end)
[/code]
Though of course if the amount of players isn't exactly one of those amounts then the speed will be set to 0.1
[editline]31st May 2016[/editline]
I should note, when I said efficiency doesn't matter, I should've specified that it doesn't matter in this case
Also - timers don't work when there are no players on the server - just sayin.
[QUOTE=Netheous;50427641]Also - timers don't work when there are no players on the server - just sayin.[/QUOTE]
That can be changed by setting sv_hibernate_think to 1
[QUOTE=Micro230;50427272]First of all - I know how to use tables I don't care about efficiency at the moment
Second of all - I've never heard of a scope I've taught myself how to program because people teach it in the stupidest ways possible
Finally - The only thing that's not showing is the function which you don't even need to know because that has nothing to do with the timer at all.[/QUOTE]
Seems like you're teaching yourself how to program in the stupidest ways possible, lol.
You're trying to use the variable as a reference. LUA doesn't support referenced variables, with the "only" exception of tables. So each time you want the speed to change, you should call [URL="http://wiki.garrysmod.com/page/timer/Adjust"]timer.Adjust[/URL] at the end of hook. Though you should probably call it when value change, so it doesn't restart the timer each tick.
To fix problem of game not finding the variable, just define the variable at the very top of file. If the variable is used in single file, put "local" in front of variable name.
[code]
local ZSpawnSpeed = 1
-- The rest of your file...
[/code]
[QUOTE=edgarasf123;50428031]You're trying to use the variable as a reference. LUA doesn't support referenced variables, with the "only" exception of tables. So each time you want the speed to change, you should call [URL="http://wiki.garrysmod.com/page/timer/Adjust"]timer.Adjust[/URL] at the end of hook. Though you should probably call it when value change, so it doesn't restart the timer each tick.
To fix problem of game not finding the variable, just define the variable at the very top of file. If the variable is used in single file, put "local" in front of variable name.
-Snip-[/QUOTE]
If he used timer.Adjust it wouldn't matter, you wouldn't need to keep it in a variable. Either way doing it in think is stupid as max players can't change after the server has started.
[editline]31st May 2016[/editline]
Also, functions are passed by referenced, and they're considered variables
The problem is that you're creating the timer before you're defining the variable.
[QUOTE=bigdogmat;50427551]Efficiency doesn't matter, readability does. Learn when you should use tables. Also, the scope of a variable is when and where it's available, Lua uses lexical scoping.
Back to your code, why are you using global variables? And why are you running those operations in a think hook? Also game.SingePlayer returns a boolean not a number.
The easy solution to this would be to use tables.
[code]
local players_to_speed = {
[2] = 4,
[4] = 4,
[8] = 3,
[12] = 2,
[16] = 2,
[32] = 1
}
local speed = game.SinglePlayer() and 0.1 or players_to_speed[game.MaxPlayers()]
timer.Create("Shieet", speed, 0, function() end)
[/code]
Though of course if the amount of players isn't exactly one of those amounts then the speed will be set to 0.1
[editline]31st May 2016[/editline]
I should note, when I said efficiency doesn't matter, I should've specified that it doesn't matter in this case[/QUOTE]
Thanks so much for the help! I ran it in a think hook because I tried others (just realized they were outdated) and they didn't work at all. Also I apologize to the people who thought I was mad and I apologize also to Netheous. I was just slightly annoyed that night so I'm sorry if I looked like I was "local imsosalty = "I must take more salt" as Tangyboxhead puts it lol.
-snipples-
Sorry, you need to Log In to post a reply to this thread.