Jump to content

type(var) == "number" isn't working


mjr

Recommended Posts

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

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

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

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

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

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