Setting a players name

I’m trying to set a players name dependant on what the value of a DLabel is.

What i’ve tried is

[lua] local NameTag = vgui.Create(‘DLabel’)
NameTag:SetParent(ClassSelect)
NameTag:SetPos(333, 95)
NameTag:SetText(’’)
NameTag:SizeToContents()
NameTag:SetTextColor(Color(255, 0, 0, 255))

        local SoldierNameEntry = vgui.Create('DTextEntry')
        SoldierNameEntry:SetParent(ClassSelect)
        SoldierNameEntry:SetSize(125, 20)
        SoldierNameEntry:SetPos(493, 60)
        SoldierNameEntry:SetText('')
        SoldierNameEntry.OnEnter = function() NameTag:SetText(SoldierNameEntry:GetValue())
                                              NameTag:SizeToContents() 
                                              end[/lua]

This is the Derma Code which is c onnected with setting the players name
Then

[lua]EnterBattle.DoClick = function() LocalPlayer():SetName(NameTag:GetValue()) end[/lua]

Enter Battle is the DButton which lets you spawn, and the NameTag works, and whatnot, just it says



lua_temp\garrycompany\gamemode\cl_init.lua:466: attempt to call method 'SetName' (a nil value) 

What would I use instead of SetName?

if your doing it for a gamemode I suggest you redefine the players name since the only way to set players name is run the console command setinfo name every frame on client

Will try, hang on for reply.

[editline]09:44PM[/editline]

It’s not working, how would you have done it?

Override the functions used to get a player’s name with your own.

Here’s an example that I can’t guarantee will work :
[lua]local meta = FindMetaTable(“Player”)

meta.oldNick = meta.Nick

function meta:Nick()
return self.CustomNick or self:oldNick()
end

function meta:SetNick(name)
if not type(name) == “string” then return end

if name == "" then
	self.CustomNick = nil
else
	self.CustomNick = name
end

end[/lua]

I think you meant to write oldNick(), not self:Nick() there.

Yeah I ninja fixed it. I don’t like having to define another metafunction though, is there a way to just pass self as an argument and it would still work?

Like :

[lua]local meta = FindMetaTable(“Player”)

local oldNick = meta.Nick

function meta:Nick()
return self.CustomNick or oldNick(self)
end[/lua]

I don’t understand meta tables that well, but i shall TRY, and probably crash and burn :frowning:

That should work.

[editline]10:00PM[/editline]

It will work.

Don’t worry, i’ll find some way of fucking it up, somehow… even though im not intending to

[lua]local meta = FindMetaTable(“Player”) – Here we’re grabbing a reference to the player metatable.

function meta:SlapHimself(damage) – Here we’re defining a method, so we can use self to refer to the player the method is being used on.
self:TakeDamage(damage or 99,self) – So we can make that player take damage
game.ConsoleCommand(self:Nick()…" slapped himself!") – And include his name in a console command.
end
[/lua]
:eng101:

So now you can use the method SlapHimself on any player anywhere in your code, for instance :
[lua]concommand.Add(“slapmydumbface”,function(ply,cmd,args) ply:SlapHimself(args[1]) end)[/lua]

[editline]05:04PM[/editline]

Cool, thanks.

Thankyou Crazy, to me they were just letters and symbols in a chunk of code D:

[editline]10:08PM[/editline]



Got Lua Message - but haven't got usermessage.IncomingMessage function!Message was: 'DSEnd'
Got Lua Message - but haven't got usermessage.IncomingMessage function!Message was: 'DSEnd'
Got Lua Message - but haven't got usermessage.IncomingMessage function!Message was: 'DSEnd'
Got Lua Message - but haven't got usermessage.IncomingMessage function!Message was: 'DSEnd'
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(Tick): No Gamemode!
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(Tick): No Gamemode!
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(DrawMonitors): No Gamemode!
GamemodeError(RenderScreenspaceEffects): No Gamemode!
GamemodeError(PostRenderVGUI): No Gamemode!
GamemodeError(Think): No Gamemode!
GamemodeError(FinishChat): No Gamemode!
GamemodeError(ShutDown): No Gamemode!
CLIENT: 252 active EHandles!


It repeated this like a million times in the console

[editline]10:13PM[/editline]

All I did was put your code in D:

WHAT HAVE YOU DONE?!

:devil:

I just tried it and got no such error.

Which file did you put it in?

the init.lua file, since its serverside

Try setting sv_scriptenforcer to 0, should fix the problem. I had it.