是否有针对本地用户的Nil值的修复程序 [英] is there a fix for Nil value for local user

查看:61
本文介绍了是否有针对本地用户的Nil值的修复程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是针对我正在开发的电话脚本,我正在尝试更新一些代码,但我一直遇到一个错误.

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.

第37行

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

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

第36行

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

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

推荐答案

解释错误

在Lua中,这是您会遇到的最常见的错误之一,因此,了解如何解决它非常重要.

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.

错误:attempt to index a nil value

要了解此错误的含义,您只需了解这些概念.

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

  • 在Lua中,只能对表进行索引(即myTable[myIndex])
  • 在Lua中,如果变量的计算结果为nil,则尝试对其进行索引,就好像它是表一样会抛出错误
  • 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

因此,应该更容易理解您收到的错误.描述此错误的一种更明确的方式将类似于"Lua解释器试图在第37行索引您的变量user但将user评估为nil".

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

在242行上,您正在调用回调并传递nil

On line 242 you are calling a callback and passing nil

cb(nil)

此回调将此nil值作为user参数发送到第37行

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

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

因此,当您尝试运行user.getPhoneNumber()时,实际上是在运行nil.getPhoneNumber(),这会引发您看到的错误.

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

1)每当使用可能为nil的变量时,请在继续操作之前创建一条if语句以检查其是否为nil.

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)确保不要将变量设置为nil.

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

1)在第36行上,创建此if语句

1) On line 36, create this if statement

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

或者像这样做类似的nil检查

Or do a similar nil check like this

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

2)始终在回调中传递用户.例如,在第242行和其他地方,传递一个实际的用户对象.

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

cb(Users[someUser])

这篇关于是否有针对本地用户的Nil值的修复程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆