• Creating a gui via server
    6 replies, posted
Im trying to open a character menu when a player joins, I decided that using net messages would be the easiest and best way to perform this, but it appears that the net message isnt working, I was wondering if anyone could shed some light on this -I would like to say, in my init I call this function, with the ply parameter, and it all works, even my console prints the correct value, its just the net isnt working Server Side: [CODE]util.AddNetworkString( "OpenCharacterMenu" ) local ply = FindMetaTable("Player") GM.Character = (GM or GAMEMODE).Character or {} function GM.Character:Initialize( ply ) local hasCharacter = ply:GetNWBool("hascharacter") if hasCharacter != "true" then print(hasCharacter) elseif hasCharacter == "true" then print(hasCharacter .. "2") net.Start( "OpenCharacterMenu" ) net.Send( ply ) end end[/CODE] Client Side: [CODE]net.Receive( "OpenCharacterMenu", function( len ) print("Test") vgui.Create("characterMenu") end )[/CODE] The reason I think the net message is messed up because console never prints test, but prints the hascharacter value... Thanks in advance
:snip: See below
[QUOTE=Skere_;51211544]You might wanna try this hook instead [url]http://wiki.garrysmod.com/page/GM/InitPostEntity[/url] This way, you don't have to network to begin with, and you're sure that the player is fully initialized clients-side. You can also network PlayerInitialSpawn, but you wanna make sure that the player is valid in that case.[/QUOTE] Why would I network the playeriinitialspawn when I could fix the current network message? Plus this function is called when on playerinitialspawn... I also tried using InitPostEntity but I dont think thats gonna work in my case...
[QUOTE=Yikegaming;51211624]Why would I network the playeriinitialspawn when I could fix the current network message? Plus this function is called when on playerinitialspawn... I also tried using InitPostEntity but I dont think thats gonna work in my case...[/QUOTE] The player possibly doesn't exist on the client yet, that'd be my guess Edit: It works perfectly fine for me, so it SHOULD work if its called by PlayerInitialSpawn [lua] if SERVER then util.AddNetworkString("test1") hook.Add("PlayerInitialSpawn", "test1", function(ply) net.Start("test1") print("debug SENDING") net.Send(ply) end) else net.Receive("test1", function(len) print("debug RECEIVING") end) end [/lua]
[QUOTE=Skere_;51211646]The player possibly doesn't exist on the client yet, that'd be my guess Edit: It works perfectly fine for me, so it SHOULD work if its called by PlayerInitialSpawn [lua] if SERVER then util.AddNetworkString("test1") hook.Add("PlayerInitialSpawn", "test1", function(ply) net.Start("test1") print("debug SENDING") net.Send(ply) end) else net.Receive("test1", function(len) print("debug RECEIVING") end) end [/lua][/QUOTE] So ive done some more testing, it appears that the net is sending, but the file with net.receive isnt receiving Serverside [CODE] util.AddNetworkString( "OpenCharacterMenu" ) function GM.Character:Initialize( ply ) local hasCharacter = ply:GetNWBool("hascharacter") if hasCharacter != "true" then print(hasCharacter) elseif hasCharacter == "true" then print(hasCharacter .. "2") net.Start( "OpenCharacterMenu" ) print("Sending Net") net.WriteBool( hasCharacter ) net.Send( ply ) end end [/CODE] Client-Side [CODE]net.Receive("OpenCharacterMenu",function( ply ) hc = net.GetBool() if hc == "true" then vgui.Create("characterMenu") end end)[/CODE] Anyone else have some help?
You got a solution. IF you are using a gamemode that adds GM.Character:Initialize( ply ) then go check their docs. Else if you are using pure gmod default functions then use [code] hook.Add("PlayerInitialSpawn", "test1", function(ply) net.Start("test1") print("debug SENDING") net.Send(ply) end) [/code] That is called once and only on the first time the player has connect to the server. If you add some other checks to see if they already have made a person then you dont send the message.
[QUOTE] [CODE]net.Receive("OpenCharacterMenu",function( ply ) hc = net.GetBool() if hc == "true" then vgui.Create("characterMenu") end end)[/CODE][/QUOTE] First of all, you read a networked bool with net.ReadBool(), not net.GetBool() Secondly, it returns a boolean (true/false), not a boolean in string form ("true"/"false") [lua] net.Receive("OpenCharacterMenu",function( len, _ ) hc = net.ReadBool if hc then vgui.Create("characterMenu") end end) [/lua]
Sorry, you need to Log In to post a reply to this thread.