• ply: Not working
    11 replies, posted
I've been lurking around the LUA forums for almost a year now and now started coding myself. I wanted to try and script a gamemode addon that makes players starve and loose energy after some time without sleep meaning they would get slower over time. I've tried searching the gmodwiki and the forums but, somehow nobody seems to have the problem i have: [b]The snippets in question:[/b] [code] if( var_energy > 70 ) then var_statstring_e = "well rested" ply:SetWalkSpeed( 300 ) ply:SetRunSpeed( 600 ) end [/code] [code] if( var_food <= 0 ) then var_statstring_f = "starving" local var_hp = ply:Health() - 2 ply:SetHealth(var_hp) end [/code] What's wrong? Everything works perfectly, no errors, but any functions that would change something with the player themself do not work. If I use Msg(ply:Name()) it gives me the correct name, too, so the player object should be pointing at people correctly... Thanks beforehand! ProudOne
did you do like "function FunctionName( ply )" with ply in it?
I'm guessing you knew enough to put this in a serverside file right? After that, your issue is that ply is never defined, so put your if statement in a function with ply in the parameters: [code] function StupidShitLOL(ply) if( var_energy > 70 ) then var_statstring_e = "well rested" ply:SetWalkSpeed( 300 ) ply:SetRunSpeed( 600 ) end end [/code]
[QUOTE=Kopimi;19644112]I'm guessing you knew enough to put this in a serverside file right? After that, your issue is that ply is never defined, so put your if statement in a function with ply in the parameters: [code] function StupidShitLOL(ply) if( var_energy > 70 ) then var_statstring_e = "well rested" ply:SetWalkSpeed( 300 ) ply:SetRunSpeed( 600 ) end end [/code][/QUOTE] I didn't put it in a serverside since i thought its affecting the client so it should go into the client's init. Just to get this perfectly straight. init.lua = Serverside cl_init.lua = Clientside shared.lua = Both Right? [b]THIS is my cl_init.lua:[/b] [code] include( 'shared.lua' ) var_food = 100 var_energy = 100 var_ecnt = 1 var_fcnt = 1 var_statstring_e = "well rested" var_statstring_f = "full" var_statstring = "You are "..var_statstring_e.." and "..var_statstring_f.."." function hud_status() frame = vgui.Create( "DFrame" ) frame:AlignLeft( 10 ) frame:AlignTop( 20 ) frame:SetSize( 400, 50 ) frame:SetTitle ( "Your Stats" ) frame.Label = vgui.Create("Label",frame) frame.Label:SetPos(5,30) frame.Label:SetSize( 595, 20) frame:SetVisible(true) frame:SetDraggable(true) frame:ShowCloseButton(false) frame.Label:SetText(var_statstring) end concommand.Add( "ha_stat", hud_status ) --//////////////////////////////////////////////////////////////////////////// Energy function fnc_stat_e( ply ) local ply = LocalPlayer() if (not ply:Alive()) then return end var_energy = var_energy-1 ply:ChatPrint( tostring(var_energy)) if( var_energy > 70 ) then var_statstring_e = "well rested" ply:SetWalkSpeed( 300 ) ply:SetRunSpeed( 600 ) end if( var_energy < 70 && var_energy > 50 ) then var_statstring_e = "rested" ply:SetWalkSpeed( 250 ) ply:SetRunSpeed( 500 ) end if( var_energy < 50 && var_energy > 30 ) then var_statstring_e = "slightly tired" ply:SetWalkSpeed( 200 ) ply:SetRunSpeed( 400 ) end if( var_energy < 30 && var_energy > 10 ) then var_statstring_e = "tired" ply:SetWalkSpeed( 150 ) ply:SetRunSpeed( 300 ) end if( var_energy < 10 && var_energy > 0 ) then var_statstring_e = "exhausted" ply:SetWalkSpeed( 100 ) ply:SetRunSpeed( 200 ) end var_statstring = "You are "..var_statstring_e.." and "..var_statstring_f.."." frame.Label:SetText(var_statstring) end --//////////////////////////////////////////////////////////////////////////// Food function fnc_stat_f( ply ) local ply = LocalPlayer() if (not ply:Alive()) then return end var_food = var_food-1 ply:ChatPrint(tostring(var_food)) if( var_food > 90 ) then var_statstring_f = "full" end if( var_food < 90 && var_food > 70 ) then var_statstring_f = "still full" end if( var_food < 70 && var_food > 60 ) then var_statstring_f = "not hungry" end if( var_food < 60 && var_food > 40 ) then var_statstring_f = "slightly hungry" end if( var_food < 40 && var_food > 30 ) then var_statstring_f = "hungry" end if( var_food < 30 && var_food > 10 ) then var_statstring_f = "very hungry" end if( var_food < 10 && var_food > 0 ) then var_statstring_f = "starving" end if( var_food <= 0 ) then var_statstring_f = "starving" local var_hp = ply:Health() - 2 ply:SetHealth(var_hp) end var_statstring = "You are "..var_statstring_e.." and "..var_statstring_f.."." frame.Label:SetText(var_statstring) end timer.Create( "foodtimer", var_fcnt, 0, fnc_stat_f, ply ) timer.Create( "energytimer", var_ecnt, 0, fnc_stat_e, ply ) [/code] [b]THIS is my init.lua:[/b] [code] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( 'shared.lua' ) function GM:PlayerInitialSpawn( ply ) ply:SetTeam( 1 ) RunConsoleCommand( "ha_stat" ) end function GM:PlayerLoadout( ply ) ply:Give( "weapon_pistol" ) ply:Give( "item_ammo_pistol" ) end [/code] [b]THIS is my shared.lua:[/b] [code] GM.Name = "Testing" GM.Author = "ProudOne" GM.Email = "" GM.Website = "" team.SetUp( 1, "Surviours", Color( 0, 125, 0, 255 ) ) [/code]
[QUOTE=ProudOne;19645076]I didn't put it in a serverside since i thought its affecting the client so it should go into the client's init. [/QUOTE] You are changing data (the player's walk/run speed) so the server must know this, too, to change their walk/run speeds accordingly.
[QUOTE=nater;19645180]You are changing data (the player's walk/run speed) so the server must know this, too, to change their walk/run speeds accordingly.[/QUOTE] Oh, okay, makes sense. So, one question remains: When i put all the code in init.lua, it keeps giving me Timer Error: ha/gamemode/init.lua:64: attempt to index local 'ply' (a nil value) So, how do i get the server to execute the code for all clients and secondly, how does the right info get to the right status panel, do i have to define the variables in shared, so both files can use them or is there some sort of GET? Thank all of you!
LocalPlayer() isn't serverside. player.SendLua, umsg.Start, RunConsoleCommand - you should look at these.
[QUOTE=Disseminate;19645723]LocalPlayer() isn't serverside. player.SendLua, umsg.Start, RunConsoleCommand - you should look at these.[/QUOTE] Oh, nifty, indeed! Thank you lots!
Basically, client -> server communication boils down to forcing console commands. Server to client is all based on usermessages (SendLua is a premade one).
[QUOTE=Disseminate;19645842]Basically, client -> server communication boils down to forcing console commands. Server to client is all based on usermessages (SendLua is a premade one).[/QUOTE] What's with datastream.StreamToServer?
[QUOTE=ProudOne;19646870]What's with datastream.StreamToServer?[/QUOTE] Even that uses console commands executed by the client - it just sends the information as arguments to some obscurely named command.
ProudOne, you need to be using serverside because the ply information is serverside. To make clientside changes you have to do it with LocalPlayer() instead of ply.
Sorry, you need to Log In to post a reply to this thread.