is there a fix for Nil value for local user

十年热恋 提交于 2019-12-13 04:35:57

问题


This is for a phone script ive been working on and im trying to update some code but I have been getting a single error.

Error @phone/server.lua:37: attempt to index a nil value (local 'user')
 ref (@phone/server.lua:37)
 handler (@framework/server/main.lua:242)
 getNumberPhone (@phone/server.lua:36)
 handler (@phone/server.lua:268)

I tried the one way I was shown here before with no luck.

line 37

function getNumberPhone(source, n)
    local n = 0 
    TriggerEvent('f:getPlayer', source, function(user)
        n = user.getPhoneNumber()
    end)
    return n
end

line 242

AddEventHandler("f:getPlayer", function(user, cb)
    if not cb then return end
    if(Users)then
        if(Users[user])then
            cb(Users[user])
        else
            cb(nil)
        end
    else
        cb(nil)
    end
end)

line 36

function getNumberPhone(source, n)
    local n = 0 
    TriggerEvent('f:getPlayer', source, function(user)
        n = user.getPhoneNumber()
    end)
    return n
end

line 286

RegisterServerEvent('gcPhone:allUpdate')
AddEventHandler('gcPhone:allUpdate', function()
    local source = source
    local identifier = GetPlayerIdentifiers(source)[1]
    TriggerClientEvent("gcPhone:myPhoneNumber", source, getNumberPhone(source))
    TriggerClientEvent("gcPhone:allMessage", source, getMessages(identifier,source))
    TriggerClientEvent("gcPhone:contactList", source, getContacts(identifier))  
end)

回答1:


Explaining the Error

In Lua, this is one of the most common errors you will run into, so it is very important for you to know how to solve it.

The error: attempt to index a nil value

To understand what this error means, you only need to understand these concepts.

  • In Lua, only tables can be indexed (i.e. myTable[myIndex])
  • In Lua, if a variable evaluates to nil, then attempting to index it as if it were a table throws an error

So it should be a little easier to understand the error you received. A more explicit way to describe this error would be something like "the Lua interpreter attempted to index your variable user on line 37 but user evaluated to nil."

Your Specific Case

On line 242 you are calling a callback and passing nil

cb(nil)

This callback sends this nil value to line 37 as the user parameter

TriggerEvent('f:getPlayer', source, function(user)
    n = user.getPhoneNumber()
end)

So when you try to run user.getPhoneNumber() you are actually running nil.getPhoneNumber(), which throws the error you saw.

Ways to fix this type of error

1) Whenever you are working with a variable that could be nil, create an if statement checking to see if it is nil before proceeding.

2) Make sure you never set that variable to nil.

Ways to fix your specific error

1) On line 36, create this if statement

TriggerEvent('f:getPlayer', source, function(user)
    if user ~= nil then
        n = user.getPhoneNumber()
    end
end)

Or do a similar nil check like this

TriggerEvent('f:getPlayer', source, function(user)
    if user then
        n = user.getPhoneNumber()
    end
end)

2) Always pass a user in your callback. For example, on line 242 and elsewhere, pass an actual user object.

cb(Users[someUser])


来源:https://stackoverflow.com/questions/58174233/is-there-a-fix-for-nil-value-for-local-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!