• Need help with RP names.
    2 replies, posted
Hey guys, I need help with rp names for my gamemode. I want an rp name system like DarkRP, where you can change the name and it'll be over your head so people can see. I have the names hovering over heads but it shows only their steam username. I've tried alot of things, none seem to work but I am a beginner. Help would be much appreciated I've made a menu to change their rp name. Here's the code that changes their name. [CODE] local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( f ) // Set parent to our "DermaPanel" DermaButton:SetText( "Submit!" ) DermaButton:SetPos( 25, 300 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () local NameString = GetGlobalString( "BN" .. LocalPlayer():SteamID() .. "" ) SetGlobalString( "BN" .. LocalPlayer():SteamID() .. "" , DermaText:GetValue() ) print( "You changed your name to " .. GetGlobalString( "BN" .. LocalPlayer():SteamID() .. "" ) ) end[/CODE] And the code that show their rp names over their heads. [CODE]function HoveringNames() for _, target in pairs(player.GetAll()) do if target:Alive() and target != LocalPlayer() then local targetPos = target:GetPos() + Vector(0,0,84) local targetDistance = math.floor((LocalPlayer():GetPos():Distance( targetPos ))/40) local targetScreenpos = targetPos:ToScreen() if targetDistance < 7 then draw.SimpleText(target:Nick(), "Trebuchet18", tonumber(targetScreenpos.x), tonumber(targetScreenpos.y), Color(200,25,25,200), TEXT_ALIGN_CENTER) end end end end hook.Add("HUDPaint", "HoveringNames", HoveringNames)[/CODE]
1. You are using ply:Nick() in the draw hook, which obviously returns their steamusername, so you want to access the GlobalString you created there. [CODE]draw.SimpleText(GetGlobalString("BN" .. target:SteamID(), target:Nick()), "Trebuchet18", tonumber(targetScreenpos.x), tonumber(targetScreenpos.y), Color(200,25,25,200), TEXT_ALIGN_CENTER)[/CODE] 2. If SetGlobalString is being run on the client, it only changes the variable for that client. In order to make it work you should use a net message to the server and change it there. Clientside code: [CODE] local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( f ) // Set parent to our "DermaPanel" DermaButton:SetText( "Submit!" ) DermaButton:SetPos( 25, 300 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () local NameString = GetGlobalString( "BN" .. LocalPlayer():SteamID() .. "" ) net.Start("change_rp_name") net.WriteString(DermaText:GetValue()) net.SendToServer() print( "You changed your name to " .. DermaText:GetValue() .. "" ) ) end [/CODE] Serverside code: [CODE] ... util.AddNetworkedString("change_rp_name") net.Receive("change_rp_name", function(len, ply) SetGlobalString( "BN" .. ply:SteamID(), net.ReadString() ) end) ... [/CODE] Please note: That code is extremely bad but it will work, you can start from there and put some checks in place on the serverside. If you are planning on using a database to store the rp name don't forget to escape the string. Also please don't use SetGlobalString, if really necessary you might want to use [URL="http://wiki.garrysmod.com/page/Entity/SetNetworkedString"]ply:SetNetworkedString(key, str)[/URL] or [URL="http://wiki.garrysmod.com/page/Entity/SetDTString"]ply:SetDTString(index, str)[/URL]
Thanks for the help!
Sorry, you need to Log In to post a reply to this thread.