Jump to content

onPlayerJoin Problem


xXGhostXx

Recommended Posts

Posted (edited)

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
Posted
function GuessShodan()

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

 

  • Confused 1
Posted (edited)

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

Posted
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 )

 

Posted
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

Posted
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 ?

Posted
11 minutes ago, Mr.Loki said:

addEventHandler("onPlayerChangeNick", getRootElement(), 
    function(oldNick, newNick, changedByUser) 
      if changedByUser then -- check if the nickname was changed by the user
          cancelEvent()
      end	
    end 
); 

 

Not Work :( 

Posted
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 )

 

Posted
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 !

Posted
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
)

 

Posted

#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

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