Jump to content

ChangeNick


Recommended Posts

  • Scripting Moderators
23 minutes ago, slapz0r said:

How to forcibly change the nickname of the player?

For example, a player logged on to the server and the nickname is changed to "Alex_Hunter"

function onPlayerLogin()
	setPlayerName(source, "Alex_Hunter")
end
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin)

Server-side.

Quote

Note that any change made to a players name with this function is not saved in their settings so the name change only lasts till they disconnect.

 

  • Like 1
Link to comment
17 hours ago, majqq said:

function onPlayerLogin()
	setPlayerName(source, "Alex_Hunter")
end
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin)

Server-side.

 

but how to do if a player changes his nickname; he is again assigned a nickname "Alex_Hunter" ?

I mean, if a player changes to some other nickname, he will again give the name Alex_Hunter

Link to comment
  • Scripting Moderators
10 hours ago, slapz0r said:

but how to do if a player changes his nickname; he is again assigned a nickname "Alex_Hunter" ?

I mean, if a player changes to some other nickname, he will again give the name Alex_Hunter

About solution above. Don't use elementdata for such a thing, or even better, don't use it at all. Use tables - they are fast and efficient.

local player_names = {}

-- Assign name to player

function onPlayerLogin()
	setPlayerName(source, "Alex_Hunter")
	player_names[source] = "Alex_Hunter"
end
addEventHandler("onPlayerLogin", getRootElement(), onPlayerLogin)

-- Avoid any changes

function onPlayerChangeNick(old, new, byuser)
	if player_names[source] then
		if old ~= new then
			setPlayerName(source, player_names[source]) -- you might replace it with cancelEvent(), it depends, read "Cancel event" note on https://wiki.multitheftauto.com/wiki/OnPlayerChangeNick
		end
	end
end
addEventHandler("onPlayerChangeNick", getRootElement(), onPlayerChangeNick)

-- Clear data from table, after player quit

function onPlayerQuit()
	if player_names[source] then
		player_names[source] = nil
	end
end
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuit)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...