timer.Create( "PayDay", 120, 0,
function( ply )
if ply:Team( ) == 1 then
GAMEMODE:AddNotify( "You have received $25.00 for your welfare check.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 25 )
elseif ply:Team( ) == 2 then
GAMEMODE:AddNotify( "You have received $30.00 for serving the city as a police officer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 30 )
elseif ply:Team( ) == 3 then
GAMEMODE:AddNotify( "You have received $50.00 for serving the city as the mayor.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 50 )
elseif ply:Team( ) == 4 then
GAMEMODE:AddNotify( "You have received $20.00 for stealing a T.V", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 20 )
elseif ply:Team( ) == 5 then
GAMEMODE:AddNotify( "You have received $25.00 for being the gundealer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 25 )
elseif ply:Team( ) == 6 then
GAMEMODE:AddNotify( "You have received $0.00 for Joining Noob", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
end
end )
(don't bitch and moan at me for not using local player, I did have LocalPlayer first, however it came with same issue)
Error: Timer Error: MRP\gamemode\shared.lua:39:attempt to index local 'ply' (a nil value)
Remove
[lua]
function( ply )
[/lua]
Replace with
[lua]
function( )
[/lua]
try
[lua]
timer.Create( "PayDay", 120, 0,
function( )
local ply = LocalPlayer()
if ply:Team( ) == 1 then
GAMEMODE:AddNotify( "You have received $25.00 for your welfare check.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash( 25 )
elseif ply:Team( ) == 2 then
GAMEMODE:AddNotify( "You have received $30.00 for serving the city as a police officer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash( 30 )
elseif ply:Team( ) == 3 then
GAMEMODE:AddNotify( "You have received $50.00 for serving the city as the mayor.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash( 50 )
elseif ply:Team( ) == 4 then
GAMEMODE:AddNotify( "You have received $20.00 for stealing a T.V", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash( 20 )
elseif ply:Team( ) == 5 then
GAMEMODE:AddNotify( "You have received $25.00 for being the gundealer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash( 25 )
elseif ply:Team( ) == 6 then
GAMEMODE:AddNotify( "You have received $0.00 for Joining Noob", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
end
end )
[/lua]
You could just use player.GetAll() and do it that way.
[lua]
timer.Create( "PayDay", 120, 0,
function( )
for _, ply in pairs(player.GetAll()) do
if ply:Team( ) == 1 then
GAMEMODE:AddNotify( "You have received $25.00 for your welfare check.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt("Money", ply:GetNWInt("Money") + 25 )
elseif ply:Team( ) == 2 then
GAMEMODE:AddNotify( "You have received $30.00 for serving the city as a police officer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt("Money", ply:GetNWInt("Money") + 30 )
elseif ply:Team( ) == 3 then
GAMEMODE:AddNotify( "You have received $50.00 for serving the city as the mayor.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt("Money", ply:GetNWInt("Money") + 50 )
elseif ply:Team( ) == 4 then
GAMEMODE:AddNotify( "You have received $20.00 for stealing a T.V", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt("Money", ply:GetNWInt("Money") + 20 )
elseif ply:Team( ) == 5 then
GAMEMODE:AddNotify( "You have received $25.00 for being the gundealer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt("Money", ply:GetNWInt("Money") + 25 )
elseif ply:Team( ) == 6 then
GAMEMODE:AddNotify( "You have received $0.00 for Joining Noob", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
end
end
end )
[/lua]
[QUOTE=jonney934;20148298]You could just use player.GetAll() and do it that way.
[lua]
timer.Create( "PayDay", 120, 0,
function( )
for _, ply in pairs(player.GetAll()) do
if ply:Team( ) == 1 then
GAMEMODE:AddNotify( "You have received $25.00 for your welfare check.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash 25 )
elseif ply:Team( ) == 2 then
GAMEMODE:AddNotify( "You have received $30.00 for serving the city as a police officer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash 30 )
elseif ply:Team( ) == 3 then
GAMEMODE:AddNotify( "You have received $50.00 for serving the city as the mayor.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash 50 )
elseif ply:Team( ) == 4 then
GAMEMODE:AddNotify( "You have received $20.00 for stealing a T.V", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash 20 )
elseif ply:Team( ) == 5 then
GAMEMODE:AddNotify( "You have received $25.00 for being the gundealer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:AddCash 25 )
elseif ply:Team( ) == 6 then
GAMEMODE:AddNotify( "You have received $0.00 for Joining Noob", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
end
end
end )
[/lua][/QUOTE]
For get that. Use
[lua]GM:Think()[/lua]
Testing it out now, I made a modification because it wasn't working first.
[QUOTE=GenieMan;20148361]Testing it out now, I made a modification because it wasn't working first.[/QUOTE]
I know who you are I have helped you before. We worked on MRP for a while. Message me on steam. I give you a GM:Think Function which is a lot better. KRALC
you forgot [code] if CLIENT then end [/code]
Since it's in shared you have to tell it to use just client or just server. The error you get in console is probably blue and not orange.
[QUOTE=GenieMan;20148412]local ply = LocalPlayer() = nil still,
[editline]02:37AM[/editline]
Jonney's method worked, thanks.[/QUOTE]
I had to message you.
[QUOTE=Terabit;20148344]For get that. Use
GM:Think()[/QUOTE]
That is a horrible idea, the Think hook is called every instant, whereas the Timer can be made to run at a specified interval. To do this interval in the Think hook you'd have to do a time comparison at the start and abort if it's too early. Why not use the built in system for delayed events rather than inefficiently hacking it into another.
I wouldn't suggest storing a whole function into a timer either. You should make a local function and call it from the timer.
[QUOTE=-TB-;20152969]I wouldn't suggest storing a whole function into a timer either. You should make a local function and call it from the timer.[/QUOTE]
That doesn't really make it more efficient, just tidier.
[QUOTE=fishface60;20152952][b]That is a horrible idea, the Think hook is called every instant, whereas the Timer can be made to run at a specified interval. To do this interval in the Think hook you'd have to do a time comparison at the start and abort if it's too early.[/b] Why not use the built in system for delayed events rather than inefficiently hacking it into another.[/QUOTE]
Actually, that is exactly how the built-in system works, as seen [url=http://luabin.foszor.com/code/lua/includes/modules/timer.lua#218]here[/url].
[QUOTE=Overv;20155616]Actually, that is exactly how the built-in system works, as seen [URL="http://luabin.foszor.com/code/lua/includes/modules/timer.lua#218"]here[/URL].[/QUOTE]
I had always assumed as much, if you're going to use it like that in any case why not use the built-in method is what I was trying to say, which I believe I did.
[QUOTE=-TB-;20152969]I wouldn't suggest storing a whole function into a timer either. You should make a local function and call it from the timer.[/QUOTE]
If you aren't calling it anywhere else then it'd be costing you a pointer's worth more memory, though this drawback is negligible compared to the gain of readability.
Though if we're going for improving style then I'd suggest something like this.
[lua]local teamPaydays = {
[1] = function(ply)
GAMEMODE:AddNotify( "You have received $25.00 for your welfare check.",
NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 25 )
end;
[2] = function(ply)
GAMEMODE:AddNotify( "You have received $30.00 for serving the city as a police officer.",
NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 30 )
end;
[3] = function(ply)
GAMEMODE:AddNotify( "You have received $50.00 for serving the city as the mayor.",
NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 50 )
end;
[4] = function(ply)
GAMEMODE:AddNotify( "You have received $20.00 for stealing a T.V",
NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 20 )
end;
[5] = function(ply)
GAMEMODE:AddNotify( "You have received $25.00 for being the gundealer.",
NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
ply:SetNWInt( "Money", ply:GetNWInt( "Money" ) + 25 )
end;
[6] = function(ply)
GAMEMODE:AddNotify( "You have received $0.00 for Joining Noob",
NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
end;
}
timer.Create( "PayDay", 120, 0, function()
for _, ply in ipairs(player.GetAll()) do
teamPaydays[ply:GetTeam()](ply)
end
end)[/lua]
Although I have doubts about the AddNotify bit, whether it'd actually send the message.
So I was on XFire with Jonney because the Paydays went and paid for about 5 seconds then it disappeared, now it's not even appearing on the HUD.
[code]GM.Name = "MultiRP Beta"
GM.Author = "Genieman"
GM.Email = "n/a"
GM.Website = "MultiRP.FreeHostia.com"
DeriveGamemode( "sandbox" )
team.SetUp( 1, "Citizen", Color( 0, 255, 0, 255 ) )
team.SetUp( 2, "Police", Color( 0, 0, 255, 255 ) )
team.SetUp( 3, "Mayor", Color( 255, 0, 0, 255 ) )
team.SetUp( 4, "Gangster", Color( 225, 255, 100, 255 ) )
team.SetUp( 5, "GunDealer", Color( 0, 255, 150, 255 ) )
team.SetUp( 6, "Joining", Color( 0, 0, 0, 0 ) )
team.SetUp( 7, "Car Builder", Color( 255, 255, 0, 255 ) )
-- gravgun
function gravgunPunt( userid, target )
if userid:IsAdmin() then
return true
else
if SERVER then DropEntityIfHeld( target ) end
return false
end
end
hook.Add( "GravGunPunt", "gravgunPunt", gravgunPunt )
--$$
timer.Create( "PayDay", 1, 0,
function( )
for _, v in pairs(player.GetAll()) do
if v:Team( ) == 1 then
GAMEMODE:AddNotify( "You have received $25.00 for your welfare check.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
v:SetNWInt(v:GetNWInt("money") + 25)
elseif v:Team( ) == 2 then
GAMEMODE:AddNotify( "You have received $30.00 for serving the city as a police officer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
v:SetNWInt(v:GetNWInt("money") + 30 )
elseif v:Team( ) == 3 then
GAMEMODE:AddNotify( "You have received $50.00 for serving the city as the mayor.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
v:SetNWInt(v:GetNWInt("money") + 50 )
elseif v:Team( ) == 4 then
GAMEMODE:AddNotify( "You have received $20.00 for stealing a T.V", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
v:SetNWInt(v:GetNWInt("money") + 20 )
elseif v:Team( ) == 5 then
GAMEMODE:AddNotify( "You have received $25.00 for being the gundealer.", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
v:SetNWInt(v:GetNWInt("money") + 25 )
elseif v:Team( ) == 6 then
GAMEMODE:AddNotify( "You have received $0.00 for Joining Noob", NOTIFY_GENERIC, 5 )
surface.PlaySound( "ambient/water/drip2.wav" )
end
end
end ) [/code]
[editline]09:13PM[/editline]
With fish's code, I get this error:
Timer Error: MRP\gamemode\shared.lua:67: attempt to call method 'GetTeam' (a nil value)
[QUOTE=GenieMan;20158850]
With fish's code, I get this error:
Timer Error: MRP\gamemode\shared.lua:67: attempt to call method 'GetTeam' (a nil value)[/QUOTE]
Ah, that'll be because I put GetTeam instead of Team. Replacing all isntances of ply:GetTeam() with ply:Team() will fix it.
I guess I'm used to assuming that if you're retrieving a value it should have 'Get' in the name, like most other functions do, and how the AccessorFunc function creates them.
How foolish I was to assume that it would follow standard programming practises؟
Still doesn't work.
I have this code:
[code]timer.Create( "payday", 60, 0, function( )
for k, v in pairs( player.GetAll( ) ) do
if v:Team() == 1 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 25 )
v:ChatPrint( "PAYDAY! Added 25$ To your wallet for being a Citizen" )
elseif v:Team() == 2 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 40 )
v:ChatPrint( "PAYDAY! Added 30$ To your wallet for being a Police Officer" )
elseif v:Team() == 3 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 50 )
v:ChatPrint( "PAYDAY! Added 50$ To your wallet for being a Mayor" )
elseif v:Team() == 4 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 20 )
v:ChatPrint( "PAYDAY! Added 20$ To your wallet for being a Gangster" )
elseif v:Team() == 5 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 25 )
v:ChatPrint( "PAYDAY! Added 25$ To your wallet for being a Gundealer" )
elseif v:Team() == 6 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 0 )
v:ChatPrint( "PAYDAY! Added 0$ To your wallet for being a Joining Noob" )
elseif v:Team() == 6 then
v:SetNWInt( "Money", v:GetNWInt( "Money" ) + 45 )
v:ChatPrint( "PAYDAY! Added 45$ To your wallet for being a Car Builder" )
end
end
end )[/code]
But say for example there's a cop, mayor, and a gundealer, each player would get a cop payday, a mayor payday, and a gundealer payday.
I can't see any obvious errors in the code. Using the table is faster and more extensible. You just need to add a new entry for the team instead of modifying the if statement.
I was bored so I redid yours as a table here. I'm just going to try and debug what you've given, although I have tested this and it appeared to match the specification.
[lua]local paydays = {
[1] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 25 )
pl:ChatPrint( "PAYDAY! Added 25$ To your wallet for being a Citizen" )
end;
[2] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 40 )
pl:ChatPrint( "PAYDAY! Added 40$ To your wallet for being a Police Officer" )
end;
[3] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 50 )
pl:ChatPrint( "PAYDAY! Added 50$ To your wallet for being a Mayor" )
end;
[4] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 20 )
pl:ChatPrint( "PAYDAY! Added 20$ To your wallet for being a Gangster" )
end;
[5] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 25 )
pl:ChatPrint( "PAYDAY! Added 25$ To your wallet for being a Gundealer" )
end;
[6] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 0 )
pl:ChatPrint( "PAYDAY! Added 0$ To your wallet for being a Joining Noob" )
end;
[7] = function(pl)
pl:SetNWInt( "Money", pl:GetNWInt( "Money", 0 ) + 45 )
pl:ChatPrint( "PAYDAY! Added 45$ To your wallet for being a Car Builder" )
end;
}
timer.Create( "payday", 60, 0, function()
for i, pl in ipairs(player.GetAll()) do
paydays[pl:Team()](pl)
end
end)[/lua]
Proof that the table is faster:
In the if statement block you perform T comparisons, where T is the player's team.
With the table it's one indexing operation.
[editline]09:11PM[/editline]
Also, the code you gave doesn't have the problem you described.
Sorry, you need to Log In to post a reply to this thread.