• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Help NPC change name

oliverarrow

Member
Joined
Jul 31, 2015
Messages
82
Reaction score
6
Why my name to change name dont work?

Show me this error when i say
change

1APe9YS.png


Script
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talk = {}
local name = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function onPlayerEndTrade(cid) npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid) npcHandler:onPlayerCloseChannel(cid) end

function creatureGreetCallback(cid)
talk[cid] = 0
name[cid] = ''
return true
end

local chars = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

RET_VALID = 1
RET_EXISTS = 2
RET_INVALID = 3

local function validName(name)
if getPlayerGUIDByName(name) ~= nil then
return RET_EXISTS
end

for i = 1, name:len() do
if not(isInArray(chars, name:sub(i,i))) then
return RET_INVALID
end
end
return RET_VALID
end

local function getValid(name, opt)
local function tchelper(first, rest)
return first:upper()..rest:lower()
end

return opt and name:gsub("(%a)([%w_']*)", tchelper) or name:gsub("^%l", string.upper)
end

local config = {
type = 'item', -- or 'item'
money = 0, -- = 1cc
everyFirstLetterCapital = false
}

function messageDefaultCallback(cid, type, msg)
if not(npcHandler:isFocused(cid)) then
return false
end

if msgcontains(msg, "change") and talk[cid] == 0 then
selfSay("Whats your new name?", cid)
talk[cid] = 1
elseif talk[cid] == 1 then
local v = getValid(msg:lower(), config.everyFirstLetterCapital)
local ret = validName(v)
if ret == RET_VALID then
selfSay("New name:'" .. v .. "' for " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "?", cid)
talk[cid], name[cid] = 2, v
else
if ret == RET_INVALID then
selfSay(msg .. " Its a invalid name. Whats your new name?", cid)
elseif ret == RET_EXISTS then
selfSay(msg .. " this name already exists. Whats your new name?", cid)
end
end
elseif talk[cid] == 2 then
if msgcontains(msg, "yes") then
if (config.type == 'money' and doPlayerRemoveMoney(cid, config.money)) or (config.type ~= 'money' and doPlayerRemoveItem(cid, config.item[1], config.item[2])) then
local curName = getPlayerName(cid)
doRemoveCreature(cid)
db.executeQuery("UPDATE players SET name = '"..name[cid].."' WHERE name = '"..curName.."';")
else
selfSay("You dont have " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "!", cid)
talk[cid] = 0
end
else
selfSay("Im not available right now, maybe later.", cid)
talk[cid] = 0
end
end


return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Go away!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, messageDefaultCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetCallback)
npcHandler:addModule(FocusModule:new())
 
It's checking for config.item, but config.item does not exist.
Code:
local config = {
type = 'item', -- or 'item'
money = 0, -- = 1cc
everyFirstLetterCapital = false
}

Your options are config.type, config.money and config.everyFirstLetterCapital.

My guess is you have to include item inside config.

like so
Code:
local config = {
type = 'item', -- or 'item'
money = 0, -- = 1cc
item = {2400, 2} --item id, count
everyFirstLetterCapital = false
}

Just change 2400 to the itemid of your choice, and the 2 to however many items are required. this could be like a doll or something I suppose.
 
This error when start server

xIj81o5.png



code:
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talk = {}
local name = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function onPlayerEndTrade(cid) npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid) npcHandler:onPlayerCloseChannel(cid) end

function creatureGreetCallback(cid)
talk[cid] = 0
name[cid] = ''
return true
end

local chars = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

RET_VALID = 1
RET_EXISTS = 2
RET_INVALID = 3

local function validName(name)
if getPlayerGUIDByName(name) ~= nil then
return RET_EXISTS
end

for i = 1, name:len() do
if not(isInArray(chars, name:sub(i,i))) then
return RET_INVALID
end
end
return RET_VALID
end

local function getValid(name, opt)
local function tchelper(first, rest)
return first:upper()..rest:lower()
end

return opt and name:gsub("(%a)([%w_']*)", tchelper) or name:gsub("^%l", string.upper)
end

local config = {
   type = 'item', -- or 'item'
   money = 0, -- = 1cc
   item = {6527, 30} --item id, count
   everyFirstLetterCapital = false
}

function messageDefaultCallback(cid, type, msg)
if not(npcHandler:isFocused(cid)) then
return false
end

if msgcontains(msg, "change") and talk[cid] == 0 then
selfSay("Whats your new name?", cid)
talk[cid] = 1
elseif talk[cid] == 1 then
local v = getValid(msg:lower(), config.everyFirstLetterCapital)
local ret = validName(v)
if ret == RET_VALID then
selfSay("New name:'" .. v .. "' for " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "?", cid)
talk[cid], name[cid] = 2, v
else
if ret == RET_INVALID then
selfSay(msg .. " Its a invalid name. Whats your new name?", cid)
elseif ret == RET_EXISTS then
selfSay(msg .. " this name already exists. Whats your new name?", cid)
end
end
elseif talk[cid] == 2 then
if msgcontains(msg, "yes") then
if (config.type == 'money' and doPlayerRemoveMoney(cid, config.money)) or (config.type ~= 'money' and doPlayerRemoveItem(cid, config.item[1], config.item[2])) then
local curName = getPlayerName(cid)
doRemoveCreature(cid)
db.executeQuery("UPDATE players SET name = '"..name[cid].."' WHERE name = '"..curName.."';")
else
selfSay("You dont have " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "!", cid)
talk[cid] = 0
end
else
selfSay("Im not available right now, maybe later.", cid)
talk[cid] = 0
end
end


return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Go away!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, messageDefaultCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetCallback)
npcHandler:addModule(FocusModule:new())
 
something along these lines should work. Configure forbiddenWords, maxWords and maxLength as you see fit
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talk = {}
local name = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function onPlayerEndTrade(cid) npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid) npcHandler:onPlayerCloseChannel(cid) end

function creatureGreetCallback(cid)
talk[cid] = 0
name[cid] = ''
return true
end

local chars = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

local forbiddenWords = {'gm', 'tutor', 'god', 'cm', 'admin', 'owner'}
local maxWords = 3
local maxLength = 20


RET_VALID = 1
RET_EXISTS = 2
RET_INVALID = 3

local function wordCount(str) 
 local count = 0
  for word in string.gmatch(str, "%a+") do
 count = count + 1
 end
  return count
end
local function validName(name)
if getPlayerGUIDByName(name) ~= nil then
return RET_EXISTS
end

for i = 1, name:len() do
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength then
return RET_INVALID
end
end
return RET_VALID
end

local function getValid(name, opt)
local function tchelper(first, rest)
return first:upper()..rest:lower()
end

return opt and name:gsub("(%a)([%w_']*)", tchelper) or name:gsub("^%l", string.upper)
end

local config = {
  type = 'item', -- or 'item'
  money = 0, -- = 1cc
  item = {6527, 30}, --item id, count
  everyFirstLetterCapital = false
}

function messageDefaultCallback(cid, type, msg)
if not(npcHandler:isFocused(cid)) then
return false
end

if msgcontains(msg, "change") and talk[cid] == 0 then
selfSay("Whats your new name?", cid)
talk[cid] = 1
elseif talk[cid] == 1 then
local v = getValid(msg:lower(), config.everyFirstLetterCapital)
local ret = validName(v)
if ret == RET_VALID then
selfSay("New name:'" .. v .. "' for " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "?", cid)
talk[cid], name[cid] = 2, v
else
if ret == RET_INVALID then
selfSay(msg .. " Its a invalid name. Whats your new name?", cid)
elseif ret == RET_EXISTS then
selfSay(msg .. " this name already exists. Whats your new name?", cid)
end
end
elseif talk[cid] == 2 then
if msgcontains(msg, "yes") then
if (config.type == 'money' and doPlayerRemoveMoney(cid, config.money)) or (config.type ~= 'money' and doPlayerRemoveItem(cid, config.item[1], config.item[2])) then
local curName = getPlayerName(cid)
doRemoveCreature(cid)
db.executeQuery("UPDATE players SET name = '"..name[cid].."' WHERE name = '"..curName.."';")
else
selfSay("You dont have " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "!", cid)
talk[cid] = 0
end
else
selfSay("Im not available right now, maybe later.", cid)
talk[cid] = 0
end
end


return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Go away!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, messageDefaultCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetCallback)
npcHandler:addModule(FocusModule:new())
 
How much cost for change name?' is more easy create an Npc selling X item and then create an talkaction changename ... and when the player changes the name of the add item... I use this method on my server
 
something along these lines should work. Configure forbiddenWords, maxWords and maxLength as you see fit
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talk = {}
local name = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function onPlayerEndTrade(cid) npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid) npcHandler:onPlayerCloseChannel(cid) end

function creatureGreetCallback(cid)
talk[cid] = 0
name[cid] = ''
return true
end

local chars = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

local forbiddenWords = {'gm', 'tutor', 'god', 'cm', 'admin', 'owner'}
local maxWords = 3
local maxLength = 20


RET_VALID = 1
RET_EXISTS = 2
RET_INVALID = 3

local function wordCount(str)
local count = 0
  for word in string.gmatch(str, "%a+") do
count = count + 1
end
  return count
end
local function validName(name)
if getPlayerGUIDByName(name) ~= nil then
return RET_EXISTS
end

for i = 1, name:len() do
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength then
return RET_INVALID
end
end
return RET_VALID
end

local function getValid(name, opt)
local function tchelper(first, rest)
return first:upper()..rest:lower()
end

return opt and name:gsub("(%a)([%w_']*)", tchelper) or name:gsub("^%l", string.upper)
end

local config = {
  type = 'item', -- or 'item'
  money = 0, -- = 1cc
  item = {6527, 30}, --item id, count
  everyFirstLetterCapital = false
}

function messageDefaultCallback(cid, type, msg)
if not(npcHandler:isFocused(cid)) then
return false
end

if msgcontains(msg, "change") and talk[cid] == 0 then
selfSay("Whats your new name?", cid)
talk[cid] = 1
elseif talk[cid] == 1 then
local v = getValid(msg:lower(), config.everyFirstLetterCapital)
local ret = validName(v)
if ret == RET_VALID then
selfSay("New name:'" .. v .. "' for " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "?", cid)
talk[cid], name[cid] = 2, v
else
if ret == RET_INVALID then
selfSay(msg .. " Its a invalid name. Whats your new name?", cid)
elseif ret == RET_EXISTS then
selfSay(msg .. " this name already exists. Whats your new name?", cid)
end
end
elseif talk[cid] == 2 then
if msgcontains(msg, "yes") then
if (config.type == 'money' and doPlayerRemoveMoney(cid, config.money)) or (config.type ~= 'money' and doPlayerRemoveItem(cid, config.item[1], config.item[2])) then
local curName = getPlayerName(cid)
doRemoveCreature(cid)
db.executeQuery("UPDATE players SET name = '"..name[cid].."' WHERE name = '"..curName.."';")
else
selfSay("You dont have " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "!", cid)
talk[cid] = 0
end
else
selfSay("Im not available right now, maybe later.", cid)
talk[cid] = 0
end
end


return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Go away!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, messageDefaultCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetCallback)
npcHandler:addModule(FocusModule:new())

Thank you so much!

But i have 2 problems

I can use so much spaces
The name separation, dont use capital letter

Sorry to my english, look the picture:
3tpDni3.png
 
Thank you so much!

But i have 2 problems

I can use so much spaces
The name separation, dont use capital letter

Sorry to my english, look the picture:
3tpDni3.png

in config it has this
Code:
everyFirstLetterCapital = false
just change it to true.
for the spaces, change
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength then
to
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, "  ") then
 
in config it has this
Code:
everyFirstLetterCapital = false
just change it to true.
for the spaces, change
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength then
to
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, "  ") then

Oh my god, you are so good, ty so much

U know how to put:
Too short words in your name.

for exemple:
16:50 You see M A O (Level 17). He has no vocation.

To set min 4 letters to names
For Exemple:
Abcd Efgh

not
Ab Cd or Abcd E or A Bcde
 
Oh my god, you are so good, ty so much

U know how to put:
Too short words in your name.

for exemple:
16:50 You see M A O (Level 17). He has no vocation.

To set min 4 letters to names
For Exemple:
Abcd Efgh

not
Ab Cd or Abcd E or A Bcde
change
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") then
to
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") or not validLen(name) then

and after
Code:
local function wordCount(str)
local count = 0
for word in string.gmatch(str, "%a+") do
count = count + 1
end
return count
end
put
Code:
local function validLen(s)
     for i in string.gmatch(s, "%a+") do
          if i:len() < minChars then
               return false
          end
     end
     return true
end
 
change
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") then
to
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") or not validLen(name) then

and after
Code:
local function wordCount(str)
local count = 0
for word in string.gmatch(str, "%a+") do
count = count + 1
end
return count
end
put
Code:
local function validLen(s)
     for i in string.gmatch(s, "%a+") do
          if i:len() < minChars then
               return false
          end
     end
     return true
end

I try this script:
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talk = {}
local name = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function onPlayerEndTrade(cid) npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid) npcHandler:onPlayerCloseChannel(cid) end

function creatureGreetCallback(cid)
talk[cid] = 0
name[cid] = ''
return true
end

local chars = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

local forbiddenWords = {'gm', 'tutor', 'god', 'cm', 'admin', 'owner'}
local maxWords = 3
local maxLength = 20


RET_VALID = 1
RET_EXISTS = 2
RET_INVALID = 3

local function wordCount(str)
 local count = 0
  for word in string.gmatch(str, "%a+") do
 count = count + 1
 end
  return count
end

local function validLen(s)
  for i in string.gmatch(s, "%a+") do
  if i:len() < minChars then
  return false
  end
  end
  return true
end

local function validName(name)
if getPlayerGUIDByName(name) ~= nil then
return RET_EXISTS
end

for i = 1, name:len() do
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") or not validLen(name) then
return RET_INVALID
end
end
return RET_VALID
end

local function getValid(name, opt)
local function tchelper(first, rest)
return first:upper()..rest:lower()
end

return opt and name:gsub("(%a)([%w_']*)", tchelper) or name:gsub("^%l", string.upper)
end

local config = {
  type = 'item', -- or 'item'
  money = 0, -- = 1cc
  item = {6527, 30}, --item id, count
  everyFirstLetterCapital = true
}

function messageDefaultCallback(cid, type, msg)
if not(npcHandler:isFocused(cid)) then
return false
end

if msgcontains(msg, "change") and talk[cid] == 0 then
selfSay("Whats your new name?", cid)
talk[cid] = 1
elseif talk[cid] == 1 then
local v = getValid(msg:lower(), config.everyFirstLetterCapital)
local ret = validName(v)
if ret == RET_VALID then
selfSay("New name:'" .. v .. "' for " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "?", cid)
talk[cid], name[cid] = 2, v
else
if ret == RET_INVALID then
selfSay(msg .. " Its a invalid name. Whats your new name?", cid)
elseif ret == RET_EXISTS then
selfSay(msg .. " this name already exists. Whats your new name?", cid)
end
end
elseif talk[cid] == 2 then
if msgcontains(msg, "yes") then
if (config.type == 'money' and doPlayerRemoveMoney(cid, config.money)) or (config.type ~= 'money' and doPlayerRemoveItem(cid, config.item[1], config.item[2])) then
local curName = getPlayerName(cid)
doRemoveCreature(cid)
db.executeQuery("UPDATE players SET name = '"..name[cid].."' WHERE name = '"..curName.."';")
else
selfSay("You dont have " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "!", cid)
talk[cid] = 0
end
else
selfSay("Im not available right now, maybe later.", cid)
talk[cid] = 0
end
end


return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Go away!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, messageDefaultCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetCallback)
npcHandler:addModule(FocusModule:new())

and now all names is invalids :S
 
Code:
01:31 M A O [17]: Tre Let
01:31 Change Name: Tre Let Its a invalid name. Whats your new name?
01:31 M A O [17]: Four Letters
01:31 Change Name: Four Letters Its a invalid name. Whats your new name?


Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talk = {}
local name = {}

function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function onPlayerEndTrade(cid) npcHandler:onPlayerEndTrade(cid) end
function onPlayerCloseChannel(cid) npcHandler:onPlayerCloseChannel(cid) end

function creatureGreetCallback(cid)
talk[cid] = 0
name[cid] = ''
return true
end

local chars = {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

local forbiddenWords = {'gm', 'tutor', 'god', 'cm', 'admin', 'owner'}
local maxWords = 3
local maxLength = 20
local minChars = 4

RET_VALID = 1
RET_EXISTS = 2
RET_INVALID = 3

local function wordCount(str)
 local count = 0
  for word in string.gmatch(str, "%a+") do
 count = count + 1
 end
  return count
end

local function validLen(s)
  for i in string.gmatch(s, "%a+") do
  if i:len() < minChars then
  return false
  end
  end
  return true
end

local function validName(name)
if getPlayerGUIDByName(name) ~= nil then
return RET_EXISTS
end

for i = 1, name:len() do
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") or not validLen(name) then
return RET_INVALID
end
end
return RET_VALID
end

local function getValid(name, opt)
local function tchelper(first, rest)
return first:upper()..rest:lower()
end

return opt and name:gsub("(%a)([%w_']*)", tchelper) or name:gsub("^%l", string.upper)
end

local config = {
  type = 'item', -- or 'item'
  money = 0, -- = 1cc
  item = {6527, 30}, --item id, count
  everyFirstLetterCapital = true
}

function messageDefaultCallback(cid, type, msg)
if not(npcHandler:isFocused(cid)) then
return false
end

if msgcontains(msg, "change") and talk[cid] == 0 then
selfSay("Whats your new name?", cid)
talk[cid] = 1
elseif talk[cid] == 1 then
local v = getValid(msg:lower(), config.everyFirstLetterCapital)
local ret = validName(v)
if ret == RET_VALID then
selfSay("New name:'" .. v .. "' for " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "?", cid)
talk[cid], name[cid] = 2, v
else
if ret == RET_INVALID then
selfSay(msg .. " Its a invalid name. Whats your new name?", cid)
elseif ret == RET_EXISTS then
selfSay(msg .. " this name already exists. Whats your new name?", cid)
end
end
elseif talk[cid] == 2 then
if msgcontains(msg, "yes") then
if (config.type == 'money' and doPlayerRemoveMoney(cid, config.money)) or (config.type ~= 'money' and doPlayerRemoveItem(cid, config.item[1], config.item[2])) then
local curName = getPlayerName(cid)
doRemoveCreature(cid)
db.executeQuery("UPDATE players SET name = '"..name[cid].."' WHERE name = '"..curName.."';")
else
selfSay("You dont have " .. (config.type == "money" and (config.money .. " gold") or (config.item[2] .. ' ' .. (config.item[2] > 1 and getItemPluralNameById(config.item[1]) or getItemNameById(config.item[1])))) .. "!", cid)
talk[cid] = 0
end
else
selfSay("Im not available right now, maybe later.", cid)
talk[cid] = 0
end
end


return true
end

npcHandler:setMessage(MESSAGE_FAREWELL, "Bye!")
npcHandler:setMessage(MESSAGE_WALKAWAY, "Go away!")
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, messageDefaultCallback)
npcHandler:setCallback(CALLBACK_FAREWELL, creatureFarewell)
npcHandler:setCallback(CALLBACK_CREATURE_DISAPPEAR, creatureFarewell)
npcHandler:setCallback(CALLBACK_GREET, creatureGreetCallback)
npcHandler:addModule(FocusModule:new())
 
in
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") or not validLen(name) then
string.find(name, " ")
this is a single space, needs to be a double space
 
in
Code:
if not(isInArray(chars, name:sub(i,i))) or isInArray(forbiddenWords, name:sub(i, i)) or wordCount(name) > maxWords or name:len() > maxLength or string.find(name, " ") or not validLen(name) then
string.find(name, " ")
this is a single space, needs to be a double space

I think it's work now, ty so much!
 
Back
Top