【问题标题】:my bindable function is spitting out the error "attempt to call a nil value"?我的可绑定函数吐出错误“尝试调用零值”?
【发布时间】:2021-08-25 01:09:40
【问题描述】:

用于 roblox lua 游戏。

我正在尝试制作一个可绑定函数,在玩家点击文本框后,输入一些内容,然后点击输入/返回,它将单词存储在服务器中,然后将其打印到输出中。现在打字方面的一切都可以工作了,它甚至可以在surfacegui上打印出来。问题是,如果我尝试运行代码,我会收到错误“尝试调用零值”。这是我在框中输入的调用代码。

local screenTextBox = script.Parent -- Location of Textbox on the screen
local partTextLabel = game.Workspace.BlueTeamGameRoom.BlueGameBoard.Board.SurfaceGui.TextBox -- Location of text on block

while true do --code to update the screengui
    wait(.5)--wait in order to keep roblox studio from crapping itself
screenTextBox.FocusLost:connect()--for clicking on the textbox
    partTextLabel.Text = screenTextBox.Text -- Change text
end


screenTextBox.FocusLost:connect(function(enterPressed) --defining enterpressed(clicking enter) within the parameters of the player clicking the box
    if enterPressed then 
        local DBallz = game.ReplicatedStorage.answerVerify:Invoke() --calling the bindable function

        print(DBallz)--printing what the bindable function returns
    end
end)

这是我的 OnInvoke 代码,用于打印框中的单词

game.ReplicatedStorage.answerVerify.OnInvoke = function()
    local Text = game.StarterGui.ScreenGui.wordSend.TextBox.Text --defining the property of the text of the textbox
    
    return Text --sending the text to the local script
end

我尝试过使用 InvokeServer 和 OnServerInvoke,但没有区别。在那里,我还尝试使用不同的方式来编写键码映射,但这也不起作用。并且调用代码位于 localScript 中,它是用户必须输入的文本框的子项。调用代码位于作为工作区子项的常规服务器端脚本中。

【问题讨论】:

  • 类似的错误带有堆栈跟踪、行号和名称。那么您要调用哪个 nil 值?请阅读How to Ask
  • @Piglet 好吧,首先,奇怪的是,错误并没有伴随堆栈跟踪、行号或名称出现,但现在看它似乎不是 nil 值是我的问题,因为如果我在文本框中的任何地方按 enter,它会说尝试调用 nil 值。即使不使用 enterPressed

标签: lua roblox


【解决方案1】:

在你的代码中我看到了这个:

screenTextBox.FocusLost:connect()

你应该在这里提供一个函数。因此,一旦您的文本框失去焦点,Roblox 就会调用恰好为 nil 的事件侦听器。

顺便说一句,您为什么要循环执行此操作?这对我来说没有意义。

【讨论】:

  • 我认为您关注的是错误的事情。您现在正在谈论的是不断更新 screengui 的代码。这就是为什么它在一个循环中。那一个有效,我遇到的问题是它下面的那个。那就是应该将单词发送到服务器的那个。我会对此发表评论以使其更清楚。
  • 那么当你将一个事件连接到一个 nil 值时,你认为会发生什么? connect 已被弃用多年。它甚至不再记录在案。请改用Connect。据我了解,一旦事件被触发,就会调用这个 nil 值。那个循环的目的是什么?多次连接事件侦听器,尤其是在无限循环中对我来说毫无意义。并且您的代码的第二部分永远不会到达,因为您陷入了无限循环。因此,如果我关注的是您代码的错误部分,您不妨将其从您的问题中删除。
  • 好吧,就像我之前说的那样,for 循环是不断地 ping 客户端,因此 surfacegui 可以随时将他们正在输入的单词放在板上。但是我想请您进一步详细说明代码陷入无限循环的意思
  • 您运行一个 while 循环,将 nil 值连接到事件(无用),更新文本(有用)并等待(-> 将控制权返回给 Roblox 一段时间)该循环之后的任何内容永远不会执行,因为您永远不会离开该循环。没有中断,条件也不会失败。
  • 好的,等等,这似乎可行。我只是想弄清楚我如何才能实现它。感谢您的帮助。
猜你喜欢
  • 2019-03-16
  • 2019-01-06
  • 2015-06-15
  • 2013-07-06
  • 1970-01-01
  • 2021-09-20
  • 2011-11-13
  • 2015-04-28
  • 1970-01-01
相关资源
最近更新 更多