Jump to content

onPlayerJoin Problem


xXGhostXx

Recommended Posts

Hi guys .

please fix my script for on players join and after /login name changed to username . before login name : Guess,random number

function GuessShodan()

   local adadshansi = math.random(9999,9999)
   setPlayerName(thePlayer, Guess#adadshansi )
end
addEventHandler("onPlayerJoin",getRootElement(),GuessShodan)

thanks.

Edited by xXGhostXx
Link to comment

Not sure what you meant but here u go.

local names = {} -- Create a tabe to save names when they join

function GuessShodan( )
	names[source] = getPlayerName( source ) -- Save the player's name in the table.
	giveRandomName( source ) -- Call our custom function to generate a random name and give it to the player.
end
addEventHandler( "onPlayerJoin", root, GuessShodan )

function giveRandomName( plr )
	local newName -- Create a variable to store the name that is only visible within the function.
	repeat 
		newName = math.random(1000,9999) -- Generate random number
	until not getPlayerFromName( "Guess_"..newName ) -- repeat the line above until no player with this name is found in the server
	setPlayerName( plr, "Guess_"..tostring(newName) ) -- Give the player the new name
end

function Restorenick( )
	if names[source] then -- Check if the player is in the table
		setPlayerName( source, names[source] ) -- Set the player's name to the nick he had when joining
		names[source] = nil -- Remove the name from the table
	end
end
addEventHandler( "onPlayerLogin", root, Restorenick )

 

Edited by Mr.Loki
Link to comment
10 hours ago, Mr.Loki said:

Not sure what you meant but here u go.


local names = {} -- Create a tabe to save names when they join

function GuessShodan( )
	names[source] = getPlayerName( source ) -- Save the player's name in the table.
	giveRandomName( source ) -- Call our custom function to generate a random name and give it to the player.
end
addEventHandler( "onPlayerJoin", root, GuessShodan )

function giveRandomName( plr )
	local newName -- Create a variable to store the name that is only visible within the function.
	repeat 
		newName = math.random(1000,9999) -- Generate random number
	until not getPlayerFromName( "Guess_"..newName ) -- repeat the line above until no player with this name is found in the server
	setPlayerName( plr, "Guess_"..tostring(newName) ) -- Give the player the new name
end

function Restorenick( )
	if names[source] then -- Check if the player is in the table
		setPlayerName( source, names[source] ) -- Set the player's name to the nick he had when joining
		names[source] = nil -- Remove the name from the table
	end
end
addEventHandler( "onPlayerLogin", root, Restorenick )

 

Worked . But players can change the name from MTA Setting ! I Want script for change the name to username /register .

Link to comment
4 hours ago, xXGhostXx said:

Worked . But players can change the name from MTA Setting ! I Want script for change the name to username /register .

You can cancel the event onPlayerChangeNick the first example on the page shows you how.

Use this instead of the above code.

function giveRandomName( )
	local newName -- Create a variable to store the name that is only visible within the function.
	repeat 
		newName = math.random(1000,9999) -- Generate random number.
	until not getPlayerFromName( "Guess_"..newName ) -- Repeat the line above until if player with this name is found.
	setPlayerName( source, "Guess_"..tostring(newName) ) -- Give the player the new name.
end
addEventHandler( "onPlayerJoin", root, giveRandomName )

function Restorenick( _, acc )
  local accountName = getAccountname(acc) -- get the name of the account they logged in with
  setPlayerName( source, accountName ) -- Set the player's name to his account name.
end
addEventHandler( "onPlayerLogin", root, Restorenick )

 

Link to comment
1 hour ago, Mr.Loki said:

You can cancel the event onPlayerChangeNick the first example on the page shows you how.

Use this instead of the above code.


function giveRandomName( )
	local newName -- Create a variable to store the name that is only visible within the function.
	repeat 
		newName = math.random(1000,9999) -- Generate random number.
	until not getPlayerFromName( "Guess_"..newName ) -- Repeat the line above until if player with this name is found.
	setPlayerName( source, "Guess_"..tostring(newName) ) -- Give the player the new name.
end
addEventHandler( "onPlayerJoin", root, giveRandomName )

function Restorenick( _, acc )
  local accountName = getAccountname(acc) -- get the name of the account they logged in with
  setPlayerName( source, accountName ) -- Set the player's name to his account name.
end
addEventHandler( "onPlayerLogin", root, Restorenick )

 

Worked but i player name changed to Guess Random Number ! i want after Login Guess and before login Set name to Username

Link to comment
2 minutes ago, Mr.Loki said:

 

what ?

12 minutes ago, xXGhostXx said:

Worked but i player name changed to Guess Random Number ! i want after Login Guess and before login Set name to Username

addEventHandler("onPlayerChangeNick", getRootElement(), 
    function() 
        cancelEvent(); 
    end 
); 

this is code ?

Link to comment
function giveRandomName( )
	local newName -- Create a variable to store the name that is only visible within the function.
	repeat 
		newName = math.random(1000,9999) -- Generate random number.
	until not getPlayerFromName( "Guess_"..newName ) -- Repeat the line above until if player with this name is found.
	setPlayerName( source, "Guess_"..tostring(newName) ) -- Give the player the new name.
end
addEventHandler( "onPlayerJoin", root, giveRandomName )

function Restorenick(_, acc)
  local accountName = getAccountName(acc) -- get the name of the account they logged in with
  setPlayerName( source, accountName ) -- Set the player's name to his account name.
end
addEventHandler( "onPlayerLogin", root, Restorenick )

 

Link to comment
7 hours ago, Dimos7 said:

function giveRandomName( )
	local newName -- Create a variable to store the name that is only visible within the function.
	repeat 
		newName = math.random(1000,9999) -- Generate random number.
	until not getPlayerFromName( "Guess_"..newName ) -- Repeat the line above until if player with this name is found.
	setPlayerName( source, "Guess_"..tostring(newName) ) -- Give the player the new name.
end
addEventHandler( "onPlayerJoin", root, giveRandomName )

function Restorenick(_, acc)
  local accountName = getAccountName(acc) -- get the name of the account they logged in with
  setPlayerName( source, accountName ) -- Set the player's name to his account name.
end
addEventHandler( "onPlayerLogin", root, Restorenick )

  

Not Work !

Link to comment
addEventHandler("onPlayerJoin", root,
	function()
		local newName = "Guest#"..math.random(1000,9999)
		repeat
			newName = "Guest#"..math.random(1000,9999)
		until not getPlayerFromName(newName)
		setPlayerName(source, newName)
	end
)

addEventHandler("onPlayerLogin", root,
	function(_,account)
		local accountName = getAccountName(account)
		setPlayerName(source, tostring(accountName))
	end
)

addEventHandler("onPlayerChangeNick", root,
	function()
		cancelEvent()
	end
)

 

Link to comment

#optimized

guests = {}

addEventHandler("onPlayerJoin", root,
	function()
		local newName = "Guest#"..math.random(9999)
		if not guests[newName] then
			setPlayerName(source, newName)
			guests[newName] = true
		end
	end
)

addEventHandler("onPlayerLogin", root,
	function(_,account)
		guests[getPlayerName(source)] = nil
		local accountName = getAccountName(account)
		setPlayerName(source, tostring(accountName))
	end
)

addEventHandler("onPlayerChangeNick", root,
	function()
		cancelEvent()
	end
)

@xXGhostXx

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...