• Timer with chat.addtext
    12 replies, posted
[CODE] timer.Create( "LvlTest", 2, 0, function() if (ply:GetNWInt( "LVLBar" ) + 1) then for k,v in pairs( player.GetAll() ) do chat.AddText( Color( 100, 100, 255 ), ply, ", Has just reached lvl ", client:GetNWInt( "LVLBar" ) ) end end end ) [/CODE] This dosen't load in chat, everytime someone reaches +1 on there INT I wan't it to advert there current NWInt, but for some reason there are no erros but player.GetAll() is not sending out it? I tried v:chat.AddText but it gave an error... even without the hole if statement and for k,v it don't work? anyone know?
[CODE] if (ply:GetNWInt( "LVLBar" ) + 1) then [/CODE] This doesn't really make sense in Lua - Lua sees that as you checking that GetNWInt is true or isn't nil, I'll give an example that I think does what you want, hold on Also, you seem to be using two variables- 'ply' and 'client'. Which one is meant to be what player? Actually, a better alternative to using a timer would be to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityNetworkedVarChanged]GM:EntityNetworkedVarChanged[/url] Try running this clientside: [CODE] hook.Add( 'EntityNetworkedVarChanged', 'showofflvl', function( ent, name, oldval, newval ) if name == 'LVLBar' and ent:IsPlayer() then -- Only run if it's the right network variable and the right kind of entity chat.AddText( Color( 100, 100, 255 ), ent, ", Has just reached lvl ", newval ) -- since this is run clientside it runs for all clients end end ) [/CODE] [editline]29th July 2016[/editline] Actually, that doesn't seem to work for me, hold on... [editline]29th July 2016[/editline] Here's a way that works using timers like you seemed to want to do: [CODE] local previouslevels = {} -- This table stores the levels from last time this timer was run timer.Create( "LvlTest", 2, 0, function() for k, ply in pairs( player.GetAll() ) do local lvl = ply:GetNWInt( "LVLBar" ) -- Get their current level if lvl ~= previouslevels[ ply ] then -- If it's different to the one we have stored then PrintMessage( HUD_PRINTTALK, ply:Nick() .. ' has just reached lvl ' .. lvl ) -- print it end previouslevels[ ply ] = lvl -- update the level to the latest one regardless whether or not it changed end end ) [/CODE] It works, but it's a bit more hacky than using a hook since it's serverside and uses a timer :/ [editline]29th July 2016[/editline] Also, it prints 0 at the start of the game when GetNWInt returns 0 because it can't retrieve the integer since it isn't set yet, so if you don't want that then just do a check to make sure lvl is not 0
[QUOTE=MPan1;50794499][CODE] if (ply:GetNWInt( "LVLBar" ) + 1) then [/CODE] This doesn't really make sense in Lua - Lua sees that as you checking that GetNWInt is true or isn't nil, I'll give an example that I think does what you want, hold on Also, you seem to be using two variables- 'ply' and 'client'. Which one is meant to be what player? Actually, a better alternative to using a timer would be to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityNetworkedVarChanged]GM:EntityNetworkedVarChanged[/url] Try running this clientside: [CODE] hook.Add( 'EntityNetworkedVarChanged', 'showofflvl', function( ent, name, oldval, newval ) if name == 'LVLBar' and ent:IsPlayer() then -- Only run if it's the right network variable and the right kind of entity chat.AddText( Color( 100, 100, 255 ), ent, ", Has just reached lvl ", newval ) -- since this is run clientside it runs for all clients end end ) [/CODE] [editline]29th July 2016[/editline] Actually, that doesn't seem to work for me, hold on... [editline]29th July 2016[/editline] Here's a way that works using timers like you seemed to want to do: [CODE] local previouslevels = {} -- This table stores the levels from last time this timer was run timer.Create( "LvlTest", 2, 0, function() for k, ply in pairs( player.GetAll() ) do local lvl = ply:GetNWInt( "LVLBar" ) -- Get their current level if lvl ~= previouslevels[ ply ] then -- If it's different to the one we have stored then PrintMessage( HUD_PRINTTALK, ply:Nick() .. ' has just reached lvl ' .. lvl ) -- print it end previouslevels[ ply ] = lvl -- update the level to the latest one regardless whether or not it changed end end ) [/CODE] It works, but it's a bit more hacky than using a hook since it's serverside and uses a timer :/ [editline]29th July 2016[/editline] Also, it prints 0 at the start of the game when GetNWInt returns 0 because it can't retrieve the integer since it isn't set yet, so if you don't want that then just do a check to make sure lvl is not 0[/QUOTE] Oh sorry, I am using CLIENT client = LocalPlayer() [editline]29th July 2016[/editline] and what does Previous levels mean? [editline]29th July 2016[/editline] oh and I sent you a private message regarding this [editline]29th July 2016[/editline] oh and I sent you a private message regarding this
[QUOTE=jacobcooper18;50794788]and what does Previous levels mean? [editline]29th July 2016[/editline] oh and I sent you a private message regarding this [editline]29th July 2016[/editline] oh and I sent you a private message regarding this[/QUOTE] I put a note next to previouslevels - it's just a table of all the levels all the players were on last time the timer was called, and you can use that table to check that the level for one player is different to the one previously stored, therefore meaning the player has reached a new level. [editline]29th July 2016[/editline] Also, the code in the message you sent me didn't even have any of the code I posted before in it.
[QUOTE=MPan1;50794823]I put a note next to previouslevels - it's just a table of all the levels all the players were on last time the timer was called, and you can use that table to check that the level for one player is different to the one previously stored, therefore meaning the player has reached a new level. [editline]29th July 2016[/editline] Also, the code in the message you sent me didn't even have any of the code I posted before in it.[/QUOTE] It dosen't work? [editline]29th July 2016[/editline] though* [editline]29th July 2016[/editline] [CODE] function InitSpawn() timer.Create( "LvlTest", 2, 0, function() for k, ply in pairs( player.GetAll() ) do local lvl = ply:GetNWInt( "LVLBar" ) -- Get their current level if lvl ~= previouslevels[ ply ] then -- If it's different to the one we have stored then chat.AddText( Color( 100, 100, 255 ), ply, ", Has just reached lvl ", lvl ) -- print it end previouslevels[ ply ] = lvl -- update the level to the latest one regardless whether or not it changed end end) end hook.Add( "PlayerInitialSpawn", "Setup", InitSpawn )[/CODE] [editline]29th July 2016[/editline] The timer dosen't test for anything?
It does work, but it only works serverside. I'm guessing your Init function is shared [editline]29th July 2016[/editline] [QUOTE=jacobcooper18;50794828]The timer dosen't test for anything?[/QUOTE] You never seem to have created the table. You have to create it outside any functions so then it can update the values in the table, e.g. [CODE] local previouslevels = {} -- create that table- it's needed function InitSpawn() timer.Create( "LvlTest", 2, 0, function() for k, ply in pairs( player.GetAll() ) do local lvl = ply:GetNWInt( "LVLBar" ) -- Get their current level if lvl ~= previouslevels[ ply ] then -- If it's different to the one we have stored then chat.AddText( Color( 100, 100, 255 ), ply, ", Has just reached lvl ", lvl ) -- print it end previouslevels[ ply ] = lvl -- update the level to the latest one regardless whether or not it changed end end) end hook.Add( "PlayerInitialSpawn", "Setup", InitSpawn ) [/CODE]
[QUOTE=MPan1;50794839]It does work, but it only works serverside. I'm guessing your Init function is shared[/QUOTE] Hmm maybe it is... but can't I just store it in NWInt? or PData?
:snip: [editline]29th July 2016[/editline] I'm getting way too confused, you can use NWInt to store the data if you want, but you can't use chat.AddText to add the text because it's a clientside function... I think I'll try and shove my original code into your code to stop this discussion from getting really confusing
[QUOTE=MPan1;50794845]:snip: [editline]29th July 2016[/editline] I'm getting way too confused, you can use NWInt to store the data if you want, but you can't use chat.AddText to add the text because it's a clientside function... I think I'll try and shove my original code into your code to stop this discussion from getting really confusing[/QUOTE] Ok.
Alright, try this. Shove this thing into a new serverside LUA file: [CODE] function InitSpawn( ply ) ply:SetNWInt( "LVLBar", ply:GetPData( "LVLBarSave" ) ) ply:SetNWInt( "XPBar", ply:GetPData( "XPBarSave" ) ) if ply:GetNWInt( "XPBar" ) == 0 or ply:GetNWInt( "LVLBar" ) == 0 then ply:SetNWInt( "XPBar", 0 ) ply:SetNWInt( "LVLBar", 1 ) else ply:SetNWInt( "LVLBar", ply:GetPData( "LVLBarSave" ) ) ply:SetNWInt( "XPBar", ply:GetPData( "XPBarSave" ) ) end end hook.Add( "PlayerInitialSpawn", "Setup", InitSpawn ) local previouslevels = {} hook.Add( "Initialize", "CreateTimer", function() timer.Create( "LvlTest", 2, 0, function() for k, ply in pairs( player.GetAll() ) do local lvl = ply:GetNWInt( "LVLBar" ) -- Get their current level if lvl ~= previouslevels[ ply ] then -- If it's different to the one we have stored then PrintMessage( HUD_PRINTTALK, ply:Nick() .. ' has just reached lvl ' .. lvl ) -- print it (chat.AddText doesn't work here since it's clientside only- if you want to use that then you'll have to move this stuff to a clientside file instead) end previouslevels[ ply ] = lvl -- update the level to the latest one regardless whether or not it changed end end ) end ) [/CODE] Then change the init file a bit so it looks like this: [CODE] AddCSLuaFile( "cl_init.lua" ) include( "cl_init.lua" ) function SetDisconnect( ply ) ply:SetPData( "LVLBarSave", ply:GetNWInt( "LVLBar" ) ) ply:SetPData( "XPBarSave", ply:GetNWInt( "XPBar" ) ) end hook.Add( "PlayerDisconnected", "SetupOnDC", SetDisconnect ) function ShutServer() for k, v in pairs( player.GetAll() ) do v:SetPData( "LVLBarSave", v:GetNWInt( "LVLBar" ) ) v:SetPData( "XPBarSave", v:GetNWInt( "XPBar" ) ) end end hook.Add( "ShutDown", "SetupOnDC", ShutServer ) function XPPerKill( npc, attacker, inflictor ) if attacker then attacker:SetNWInt( "XPBar", attacker:GetNWInt( "XPBar" ) + 10 ) if attacker:GetNWInt( "XPBar" ) == 100 then attacker:SetNWInt( "LVLBar", attacker:GetNWInt( "LVLBar" ) + 1 ) attacker:SetNWInt( "XPBar", 0) end end end hook.Add( "OnNPCKilled", "NPCXP", XPPerKill ) [/CODE] Hopefully then it'll work, I think it will. Network integers don't need to be created shared to work, only on the server. [editline]29th July 2016[/editline] Sorry to spoonfeed, but with your previous code it's a bit hard to explain what to do unless I just show it to you
[QUOTE=MPan1;50794866]Alright, try this. Shove this thing into a new serverside LUA file: [CODE] function InitSpawn( ply ) ply:SetNWInt( "LVLBar", ply:GetPData( "LVLBarSave" ) ) ply:SetNWInt( "XPBar", ply:GetPData( "XPBarSave" ) ) if ply:GetNWInt( "XPBar" ) == 0 or ply:GetNWInt( "LVLBar" ) == 0 then ply:SetNWInt( "XPBar", 0 ) ply:SetNWInt( "LVLBar", 1 ) else ply:SetNWInt( "LVLBar", ply:GetPData( "LVLBarSave" ) ) ply:SetNWInt( "XPBar", ply:GetPData( "XPBarSave" ) ) end end hook.Add( "PlayerInitialSpawn", "Setup", InitSpawn ) local previouslevels = {} hook.Add( "Initialize", "CreateTimer", function() timer.Create( "LvlTest", 2, 0, function() for k, ply in pairs( player.GetAll() ) do local lvl = ply:GetNWInt( "LVLBar" ) -- Get their current level if lvl ~= previouslevels[ ply ] then -- If it's different to the one we have stored then PrintMessage( HUD_PRINTTALK, ply:Nick() .. ' has just reached lvl ' .. lvl ) -- print it end previouslevels[ ply ] = lvl -- update the level to the latest one regardless whether or not it changed end end ) end ) [/CODE] Then change the init file a bit so it looks like this: [CODE] AddCSLuaFile( "cl_init.lua" ) include( "cl_init.lua" ) function SetDisconnect( ply ) ply:SetPData( "LVLBarSave", ply:GetNWInt( "LVLBar" ) ) ply:SetPData( "XPBarSave", ply:GetNWInt( "XPBar" ) ) end hook.Add( "PlayerDisconnected", "SetupOnDC", SetDisconnect ) function ShutServer() for k, v in pairs( player.GetAll() ) do v:SetPData( "LVLBarSave", v:GetNWInt( "LVLBar" ) ) v:SetPData( "XPBarSave", v:GetNWInt( "XPBar" ) ) end end hook.Add( "ShutDown", "SetupOnDC", ShutServer ) function XPPerKill( npc, attacker, inflictor ) if attacker then attacker:SetNWInt( "XPBar", attacker:GetNWInt( "XPBar" ) + 10 ) if attacker:GetNWInt( "XPBar" ) == 100 then attacker:SetNWInt( "LVLBar", attacker:GetNWInt( "LVLBar" ) + 1 ) attacker:SetNWInt( "XPBar", 0) end end end hook.Add( "OnNPCKilled", "NPCXP", XPPerKill ) [/CODE] Hopefully then it'll work, I think it will. Network integers don't need to be created shared to work, only on the server. [editline]29th July 2016[/editline] Sorry to spoonfeed, but with your previous code it's a bit hard to explain what to do unless I just show it to you[/QUOTE] It's ok, SpoonFeed helps me learn aswell, I read over the code and edit it. And it works, but the only problem with it is: It gets the original LEVEL (Before it is changed) and broadcasts it in chat? And then it does the same but with the new level? Woops, I guess it does that when its inserting the valuables.. after that it works perfectly! Thanks MPan1 have a Lua King!
[QUOTE=jacobcooper18;50794882]And it works, but the only problem with it is: It gets the original LEVEL (Before it is changed) and broadcasts it in chat? And then it does the same but with the new level?[/QUOTE] [QUOTE=MPan1;50794499]Also, it prints 0 at the start of the game when GetNWInt returns 0 because it can't retrieve the integer since it isn't set yet, so if you don't want that then just do a check to make sure lvl is not 0[/QUOTE] You just need to do a simple check to make sure the level isn't 0, like [CODE] if lvl ~= 0 and lvl ~= previouslevels[ ply ] then [/CODE]
[QUOTE=MPan1;50794887]You just need to do a simple check to make sure the level isn't 0, like [CODE] if lvl ~= 0 and lvl ~= previouslevels[ ply ] then [/CODE][/QUOTE] Thanks
Sorry, you need to Log In to post a reply to this thread.