I've ran into problems trying to change a name in lua.
[lua]
function setName()
local pMeta = FindMetaTable("Player")
function pMeta:Nick()
return "NewName"
end
function pMeta:Name()
return "NewName"
end
print(pMeta:Nick())
print(pMeta:Name())
end
[/lua]
This shared function is able to print the 'NewName' twice, but in-game there is no difference (in scoreboard & playersay ect.)
Most things I've found were outdated sadly.
Any help is appreciated!
I'm not sure why you encapsulated the code in your setName function, but once it's been ran it should start that override. Did you run it on both client and server to test?
I'd do it like this in a shared file:
[lua]
local nameOverrides = {}
local pMeta = FindMetaTable("Player")
local oldNick = pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or oldNick(self)
end
local oldName = pMeta.Name
function pMeta:Name()
return nameOverrides[self] or oldName(self)
end
function pMeta:SetName(name)
nameOverrides[self] = name
end
[/lua]
and to use it:
player.GetByID(1):SetName("dickbag")
but you'll need to network stuff if you want to run the command serverside and see the change clientside
[QUOTE=bitches;49369271]I'm not sure why you encapsulated the code in your setName function, but once it's been ran it should start that override. Did you run it on both client and server to test?
I'd do it like this in a shared file:
[lua]
local nameOverrides = {}
local pMeta = FindMetaTable("Player")
local oldNick = pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or self:oldNick()
end
local oldName = pMeta.Name
function pMeta:Name()
return nameOverrides[self] or self:oldName()
end
function pmeta:SetName(name)
nameOverrides[self] = name
end
[/lua]
and to use it:
player.GetByID(1):SetName("dickbag")
but you'll need to network stuff if you want to run the command serverside and see the change clientside[/QUOTE]
Attempt to call method oldNick, a nil value.
And had another error but that was fixed (pmeta should have been pMeta in the third function)
It spams the console with this error
The error refers to this line:
[lua] return nameOverrides[self] or self:oldNick()[/lua]
This will prevent overriding the original function.
[lua]local nameOverrides = {}
local pMeta = FindMetaTable("Player")
pMeta.oldNick = pMeta.oldNick or pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or self:oldNick()
end
pMeta.oldName = pMeta.oldName or pMeta.Name
function pMeta:Name()
return nameOverrides[self] or self:oldName()
end
function pmeta:SetName(name)
nameOverrides[self] = name
end[/lua]
[QUOTE=Skere_;49369370]Attempt to call method oldNick, a nil value.
And had another error but that was fixed (pmeta should have been pMeta in the third function)
It spams the console with this error
The error refers to this line:
[lua] return nameOverrides[self] or self:oldNick()[/lua][/QUOTE]
change self:oldNick() to oldNick(self)
my bad (oldNick isn't part of the player meta, but calling the copied function with the player as the first parameter is equivalent)
[editline]22nd December 2015[/editline]
[QUOTE=Nak;49369436]This will prevent overriding the original function.
[lua]local nameOverrides = {}
local pMeta = FindMetaTable("Player")
pMeta.oldNick = pMeta.oldNick or pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or self:oldNick()
end
pMeta.oldName = pMeta.oldName or pMeta.Name
function pMeta:Name()
return nameOverrides[self] or self:oldName()
end
function pmeta:SetName(name)
nameOverrides[self] = name
end[/lua][/QUOTE]
late and bad
don't add functions to the meta if you can avoid it, to avoid addon conflicts
[QUOTE=bitches;49369481]late and bad
don't add functions to the meta if you can avoid it, to avoid addon conflicts[/QUOTE]
Wouldn't it be better to store the old function it in a global variable, to prevent functions stocking up?
Also if you make a mistake .. you have to restart to get the original function when you localize it.
[QUOTE=bitches;49369481]change self:oldNick() to oldNick(self)
my bad (oldNick isn't part of the player meta, but calling the copied function with the player as the first parameter is equivalent)
[editline]22nd December 2015[/editline]
late and bad
don't add functions to the meta if you can avoid it, to avoid addon conflicts[/QUOTE]
Worked well, thanks a lot!
[B]EDIT:[/B] It still doesn't work in the chatbox, it works anywhere else though (Called it on both serverside & clientside)
The chatbox's lines are added using the function [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/chat/AddText]chat.AddText[/url] which may be using hardcoded steam player data instead of ply:Nick().
You could override your gamemode's version of [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnPlayerChat]GM/OnPlayerChat[/url] to replace the player object insertion with team color, overridden name, and the prior set color, or you could try overriding [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/chat/AddText]chat.AddText[/url] but that would be a mess.
If your gamemode were sandbox, the original sandbox OnPlayerChat function is:
[lua]
function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, player )
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
return true
end[/lua]
The edited version:
[lua]
function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, team.GetColor(player:Team()) //this line was added
table.insert( tab, player:Nick() ) //this line was changed
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
return true
end[/lua]
But instead of overriding the gamemode itself you can add it to your existing shared file as a hook, as client code:
[lua]
local nameOverrides = {}
local pMeta = FindMetaTable("Player")
local oldNick = pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or oldNick(self)
end
local oldName = pMeta.Name
function pMeta:Name()
return nameOverrides[self] or oldName(self)
end
function pMeta:SetName(name)
nameOverrides[self] = name
end
if CLIENT then
hook.Add("OnPlayerChat","NamesOverride",function(player, strText, bTeamOnly, bPlayerIsDead)
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, team.GetColor(player:Team()) //this line was added
table.insert( tab, player:Nick() ) //this line was changed
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
return true
end)
end
[/lua]
This still doesn't do any networking, meaning that if the server runs this code, the client won't see changed names.
[editline]22nd December 2015[/editline]
[QUOTE=Nak;49369547]Wouldn't it be better to store the old function it in a global variable, to prevent functions stocking up?
Also if you make a mistake .. you have to restart to get the original function when you localize it.[/QUOTE]
Never globalize unless you need the variable in another script. Which you don't in this case. Doing so has no benefits and only risks other scripts overwriting it.
[QUOTE=bitches;49369823]The chatbox's lines are added using the function [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/chat/AddText]chat.AddText[/url] which may be using hardcoded steam player data instead of ply:Nick().
You could override your gamemode's version of [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnPlayerChat]GM/OnPlayerChat[/url] to replace the player object insertion with team color, overridden name, and the prior set color, or you could try overriding [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/chat/AddText]chat.AddText[/url] but that would be a mess.
If your gamemode were sandbox, the original sandbox OnPlayerChat function is:
[lua]
function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, player )
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
return true
end[/lua]
The edited version:
[lua]
function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, team.GetColor(player:Team()) //this line was added
table.insert( tab, player:Nick() ) //this line was changed
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
return true
end[/lua]
But instead of overriding the gamemode itself you can add it to your existing shared file as a hook, as client code:
[lua]
local nameOverrides = {}
local pMeta = FindMetaTable("Player")
local oldNick = pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or oldNick(self)
end
local oldName = pMeta.Name
function pMeta:Name()
return nameOverrides[self] or oldName(self)
end
function pMeta:SetName(name)
nameOverrides[self] = name
end
if CLIENT then
hook.Add("OnPlayerChat","NamesOverride",function(player, strText, bTeamOnly, bPlayerIsDead)
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, team.GetColor(player:Team()) //this line was added
table.insert( tab, player:Nick() ) //this line was changed
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
return true
end)
end
[/lua]
This still doesn't do any networking, meaning that if the server runs this code, the client won't see changed names.
[editline]22nd December 2015[/editline]
Never globalize unless you need the variable in another script. Which you don't in this case. Doing so has no benefits and only risks other scripts overwriting it.[/QUOTE]
For some reason that disables chat completely, thanks for doing the research though (and yes the gamemode is derived from Sandbox)
Even if I use the old original function it won't display anything in chat
[QUOTE=bitches;49369823]The chatbox's lines are added using the function [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/chat/AddText]chat.AddText[/url] which may be using hardcoded steam player data instead of ply:Nick().[/QUOTE]
Returning in that hook suppresses the message
[code]
local nameOverrides = {}
local pMeta = FindMetaTable("Player")
local oldNick = pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or oldNick(self)
end
local oldName = pMeta.Name
function pMeta:Name()
return nameOverrides[self] or oldName(self)
end
function pMeta:SetName(name)
nameOverrides[self] = name
end
if CLIENT then
hook.Add("OnPlayerChat","NamesOverride",function(player, strText, bTeamOnly, bPlayerIsDead)
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, team.GetColor(player:Team()) //this line was added
table.insert( tab, player:Nick() ) //this line was changed
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
end)
end
[/code]
[QUOTE=Coffeee;49370470]Returning in that hook suppresses the message
[code]
local nameOverrides = {}
local pMeta = FindMetaTable("Player")
local oldNick = pMeta.Nick
function pMeta:Nick()
return nameOverrides[self] or oldNick(self)
end
local oldName = pMeta.Name
function pMeta:Name()
return nameOverrides[self] or oldName(self)
end
function pMeta:SetName(name)
nameOverrides[self] = name
end
if CLIENT then
hook.Add("OnPlayerChat","NamesOverride",function(player, strText, bTeamOnly, bPlayerIsDead)
local tab = {}
if ( bPlayerIsDead ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "*DEAD* " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "(TEAM) " )
end
if ( IsValid( player ) ) then
table.insert( tab, team.GetColor(player:Team()) //this line was added
table.insert( tab, player:Nick() ) //this line was changed
else
table.insert( tab, "Console" )
end
table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )
chat.AddText( unpack(tab) )
end)
end
[/code][/QUOTE]
Still won't do anything in chat :/
I'll do it in a different way then
Sorry, you need to Log In to post a reply to this thread.