Jump to content

attempt to index field '?' (a nil value)


Bilal135

Recommended Posts

local crewData = {};

function createCrew(crew, tag, r, g, b)
    if not tostring(crew) or not tostring(tag) or not tonumber(r) or not tonumber(g) or not tonumber(b) then
        return outputDebugString("* createCrew: Wrong args.", 1)
    end
    if doesCrewExist(crew) then
        return outputDebugString("* createCrew: Crew already exists.", 1)
    end
    crewData[crew] = {
        Members = {},
        crewTag = tag,
        Color = {r, g, b},
        Leader = {},
        Experience = 0,
    };
    outputDebugString(crew.." successfully created.")
    return crew
end

-- getPlayerCrew
function getPlayerCrew(player)
    if not player then 
        return outputDebugString("* getPlayerCrew: Wrong args.", 1)
    end
    local account = getPlayerAccount(player)
    if isGuestAccount(account) then return false end
    local accountName = getAccountName(account)
    for k, v in pairs(crewData) do
        for k1, v1 in pairs(crewData[v].Members) do -- Error: attempt to index field '?' (a nil value)
            if v1 == accountName then
                return v
            end
        end
    end
    return false
end 

-- addPlayer
function addPlayer(player, crew)
    if not player or not tostring(crew) then 
        return outputDebugString("* addPlayer: Wrong args.", 1)
    end
    local account = getPlayerAccount(player)
    local accountName = getAccountName(account)
    if isGuestAccount(account) then return end
    if getPlayerCrew(player) then return end -- getPlayerCrew is called here.
    table.insert(crewData[crew].Members, accountName)
    return true
end

I've been scratching my head over this error for a while now. crewData[v]. Members should not be nil. I've tested that by adding a few test strings in Members table (in addPlayer function), and then by this command. It does output the strings, but still gives error when getPlayerCrew is called.

function listMembers(player, cmd, crew)
    for k, v in pairs(crewData[crew].Members) do
        outputChatBox(v, root)
    end
end
addCommandHandler("fmem", listMembers)

Thanks.

Edited by Bilal135
Link to post
3 hours ago, Bilal135 said:

Members should not be nil. I've tested that by adding a few test strings

If that error shows up, it isn't only Members that is nil, but also the crew (sub-)table.

 

Why don't you use your function? You can never trust data that has been inputted by a user after all.

>

doesCrewExist(crew)

 

 

 

 

 

  • Like 1
Link to post

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