mjr Posted May 20, 2012 Share Posted May 20, 2012 working on PM function now. problem is that 6th string of code isn't working. even when i write /pm 1 msg, "if" statement anyway goes to type(recipient) == "string" case. what's the problem? function privateMessage(source,commandName,recipient,...) local origin = { ... } local message = table.concat(origin, "") if type(recipient) == "string" then target = (getPlayerByName (recipient)) elseif type(recipient) == "number" then for i, v in ipairs (getElementsByType("players")) do if recipient == getElementData(v,"id") then target = v break end end end [...immaterial code...] end Link to comment
Jaysds1 Posted May 20, 2012 Share Posted May 20, 2012 try this: function privateMessage(source,commandName,recipient,...) local origin = { ... } local message = table.concat(origin, "") if (type(recipient) == "string") then target = getPlayerFromName(recipient) --You've used getPlayerByName, it's getPlayerFromName elseif (type(recipient) == "number") then for i, v in pairs (getElementsByType("player")) do -- You've put players, instead of player if recipient == getElementData(v,"id") then target = v break end end end [...immaterial code...] end EDIT: Added Comments Link to comment
TwiX! Posted May 20, 2012 Share Posted May 20, 2012 function privateMessage(source,commandName,recipient,...) local origin = { ... } local message = table.concat( origin, " " ) if recipient then if tonumber(recipient) then target = getPlayerFromID(tonumber(recipient)) else target = getPlayerNameWithoutHexColors(recipient) end --HERE YOU NEED CODE WITH MESSAGES else outputChatBox("* Error", source, 255, 255, 255, true) return false end end addCommandHandler("pm", privateMessage) function getPlayerNameWithoutHexColors(player) local thePlayer = getPlayerFromName(player) if thePlayer then return thePlayer end for _,thePlayer in ipairs(getElementsByType("player")) do if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), player:lower(), 1, true) then return thePlayer end end return false end function getPlayerFromID(theID) theID = tonumber(theID) if theID then local theplayer players = getElementsByType("player") for id,p in ipairs(players) do if theID == id then theplayer = p end end return theplayer else return false end end Link to comment
mjr Posted May 20, 2012 Author Share Posted May 20, 2012 --You've used getPlayerByName, it's getPlayerFromName getPlayerByName - my function that is similar to standart getPlayerFromName, but in addition it converts any coloured name to normal form -- You've put players, instead of player okay, my mistake, but it isn't what try to solve. variable "recipient" always is a "string" type. Even if i type number instead on nichname. type(recipient) == "string" triggers all the time. Statement don't want to progress. Link to comment
Jaysds1 Posted May 20, 2012 Share Posted May 20, 2012 if you're typing in a command then it's automatically a string Link to comment
mjr Posted May 20, 2012 Author Share Posted May 20, 2012 if tonumber(recipient) then for i, v in ipairs (getElementsByType("player")) do if recipient == getElementData(v,"id") then target = v break end end else target = (getPlayerByName (recipient)) end don't work too /pm 1 msg --player not found getElementData(source,"id") --1 Link to comment
Jaysds1 Posted May 20, 2012 Share Posted May 20, 2012 try this: if tonumber(recipient) then for i, v in pairs (getElementsByType("player")) do if recipient == getElementData(v,"id") then target = v break end end else target = getPlayerByName (recipient) end Link to comment
mjr Posted May 20, 2012 Author Share Posted May 20, 2012 haha, same code, same result Link to comment
Anderl Posted May 20, 2012 Share Posted May 20, 2012 if ( tonumber ( recipient ) ) then for _, pPlayer in ipairs ( getElementsByType 'player' ) do if ( tonumber ( recipient ) == getElementData ( pPlayer, 'id' ) ) then local pTarget = pPlayer; break; end end else local pTarget = getPlayerByName ( recipient ); end Link to comment
mjr Posted May 20, 2012 Author Share Posted May 20, 2012 thx, Dwane. if tonumber(recipient) == getElementData (v,"id") works! final form: function privateMessage(source,commandName,recipient,...) local origin = { ... } local message = table.concat(origin, "") if tonumber(recipient) then for i, v in ipairs (getElementsByType("player")) do if tonumber(recipient) == getElementData(v,"id") then target = v break end end else target = (getPlayerByName (recipient)) end [...immaterial code...] target = nil end thanks to everybody for help! Link to comment
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