• Using a variable over files
    8 replies, posted
I got a little problem : I'm trying to use a variable named "Job" in cl_init (to make the HUD with it) that can only be set in init (I need to use player:Team()). When I try to run it, it tells me, in short, that I have no value for Job Here's the code around the "job" Var (and another variable that don't work either) : init.lua function GM:PlayerLoadout(ply) --"The weapons/items that the player spawns with" function ply:StripWeapons() -- This command strips all weapons from the player. if ply:Team() == 1 then --If the player is on team "Guest"... ply:Give("weapon_physcannon") -- ...then give them the Gravity Gun. job = "Peasent" redval = 20 greenval = 200 blueval = 20 alphaval = 255 ect... cl_init.lua draw.DrawText("Job : " .. job, "ScoreboardText", 25, ScrH() - 110, Color( redval, greenval, blueval, alphaval) , TEXT_ALIGN_LEFT ) Like I said, cl_init.lua can't use the "job" Var that is defined in init.lua And yes, I used the Gmod wiki templates, and couldn't be bothered to remove comments
Lua doesn't magically network all variables for you, you need to network them yourself. [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexd325.html?title=User_Messages[/url] Also in your code you're setting a lot of global variables that really are meant for one player.
[QUOTE=leiftiger;35244406]Also in your code you're setting a lot of global variables that really are meant for one player.[/QUOTE] Well, I'm a beginner, so as long as it works, it's ok for me :/
[QUOTE=dylanstrategie;35244454]Well, I'm a beginner, so as long as it works, it's ok for me :/[/QUOTE] It won't work though.
I did what you asked, and I get this error : [lua\includes\modules\usermessage.lua:87] attempt to call field 'Function' (a nil value) Here's the init and cl_init code init.lua function team_peasent( ply ) ply:SetTeam( 1 ) //Make the player join team 1 umsg.Start( "team_hudinfo" ) umsg.String( "Peasent" ) umsg.Long( 20 ) umsg.Long ( 200 ) umsg.Long ( 20 ) umsg.Long ( 255 ) umsg.End() ply:Spawn() end cl_init.lua local function Recv_team_hudinfo( data1 ) local job = data1:ReadString() local redval = data1:ReadLong() local greenval = data1:ReadLong() local blueval = data1:ReadLong() local alphaval = data1:ReadLong() end
Assuming the job title and job colors you're using for each job are the same for every player who will be in that job you could register that sort of information using the Team library (just make sure it's in a shared file). [b][url=http://wiki.garrysmod.com/?title=Team.SetUp]Team.SetUp [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] e.g. [code]team.SetUp (1, "Peasants", Color (20, 200, 20, 255))[/code] EDIT: Then you can do: [code]draw.DrawText("Job : " .. team.GetName(LocalPlayer():Team()),"ScoreboardText", 25, ScrH() - 110, team.GetColor(LocalPlayer():Team())) , TEXT_ALIGN_LEFT ) [/code]
Already got that in my shared.lua : function GM:CreateTeams() if ( !self.TeamBased ) then return end TEAM_PEASENT = 1 team.SetUp( TEAM_PEASENT, "Peasent", Color( 20, 200, 20, 255 ) ) team.SetSpawnPoint( TEAM_PEASENT, "info_player_start" ) Edit : [gamemodes\medieval_rp\gamemode\cl_init.lua:45] unexpected symbol near ',' for your add Edit 2 : Fixed it, however the code for teams itself is wrong (No team, base yellow color) Code in shared.lua : function GM:CreateTeams() if ( !self.TeamBased ) then return end TEAM_PEASENT = 1 team.SetUp( TEAM_PEASENT, "Peasent", Color( 20, 200, 20, 255 ) ) team.SetSpawnPoint( TEAM_PEASENT, "info_player_start" ) TEAM_BANDIT = 2 team.SetUp( TEAM_BANDIT, "Bandit", Color( 0, 0, 0, 100 ) ) team.SetSpawnPoint( TEAM_BANDIT, "info_player_start" ) TEAM_GUARD = 3 team.SetUp( TEAM_GUARD, "Guard", Color (20, 20, 200, 255) ) team.SetSpawnPoint( TEAM_GUARD, "info_player_start" ) end
Then you can use the draw.DrawText line I created above, since the teams information is now shared, the team name and color exists on the server and on the client and you won't need to network it yourself. Let me know if it doesn't work
Alright, I fixed all my code, everything is working for now. Thank you
Sorry, you need to Log In to post a reply to this thread.