proracer Posted January 2, 2011 Posted January 2, 2011 Okay I'm trying to test when someone enters server with nick: "Player" it sets random nick from: "Player1 to "Player1000". But it doesn't work with too many errors. function randomPlayerNick() local joinedPlayerName = getPlayerName (source) local newPlayerName = math.random (Player1, Player1000) if getPlayerName == "Player" then do setPlayerName ( thePlayer, newPlayerName ) elseif getPlayerName ~== "Player" then return end end addEventHandler ( "onPlayerJoin", getRootElement(), randomPlayerNick) Im using MTA Script Editor so it's easier to find errors.One error is that I need to end "do" statement and with "end" but I don't know how.Every time I put "end" somewhere it closes statement "if".Just too many errors, please any help will be appreciated. Still learning.
Castillo Posted January 2, 2011 Posted January 2, 2011 function renamePlayer () if ( string.lower ( getPlayerName ( source ) ) == string.lower ( "Player" ) ) then local name = "Player_" .. math.random ( 1, 99 ) while getPlayerFromName ( name ) do local name = "Player_" .. math.random ( 1, 99 ) end setPlayerName ( source, name ) end end addEventHandler ( "onPlayerJoin", getRootElement(), renamePlayer )
proracer Posted January 2, 2011 Author Posted January 2, 2011 Thx for this but can you please put comments because I don't just want script, I wanna learn.I wanna understand why is that .Thx for help
Castillo Posted January 2, 2011 Posted January 2, 2011 Too lazy to post what was wrong on your code (i didn't sleep yet)
Aibo Posted January 2, 2011 Posted January 2, 2011 Thx for this but can you please put comments because I don't just want script, I wanna learn.I wanna understand why is that .Thx for help here: function renamePlayer() -- first we check if player name is "Player" or "pLayEr", by lowering the case -- using string.lower() and comparing the result with "player": if string.lower(getPlayerName(source)) == "player" then -- then we generate a string with new name like "Player_35" and put it in local variable: local name = "Player_" .. math.random(1, 99) -- this will generate a new name if a player with previously generated name already exists: while getPlayerFromName(name) do name = "Player_" .. math.random(1, 99) end -- setting a new player name from local name variable: setPlayerName(source, name) end end addEventHandler ( "onPlayerJoin", getRootElement(), renamePlayer)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now