【问题标题】:What does "attempt to index function with 'Connect'" mean in Roblox?Roblox 中的“尝试使用 'Connect' 索引函数”是什么意思?
【发布时间】:2021-03-29 14:57:05
【问题描述】:
local Player = game.Players.LocalPlayer
local PlayerCash = Player:WaitForChild("leaderstats"):WaitForChild("Strength")
local Mouse = Player:GetMouse()
local amount = 50

script.Parent.Activate:Connect(function()
game.Players.LocalPlayer.leaderstats.money.Value = game.Players.LocalPlayer.leaderstats.money.Value  + amount
end)

我正在尝试编写代码以在您单击时给予力量。当我按“播放”时,它不起作用。它在输出中所说的只是 '尝试使用 'Connect' 索引函数。'

【问题讨论】:

  • 嘿嘿,请不要发你的代码图片,请将代码复制粘贴到这里,方便大家讨论。
  • 我改了:)

标签: lua roblox


【解决方案1】:

错误“尝试使用 [字段名称] 索引 [类型]”意味着您错误地使用了对象之类的东西,并且您正在尝试访问其上的某些字段。

在您的情况下,此错误指向以下行:script.Parent.Activate:Connect。这表示script.Parent.Activate 是一个函数而不是一个表,您不能在函数上调用Connect。这只是为了帮助我们找出我们的代码出了什么问题。

由于您使用的是Tools,因此有一个简单的修复方法。您正在寻找Activated 事件,而不是使用Activate 函数。所以只需像这样更改更新该行:

script.Parent.Activated:Connect(function()

【讨论】:

  • 谢谢!该脚本现在一切正常,这一切都归功于您!
【解决方案2】:

它将函数连接到发生的事件,如下所示:

local tool = script.Parent

 -- It connects the event (the tool being equipped) to a function.
tool.Equipped:Connect(function()
  -- CODE
end)

此外,上面的答案是如何解决您的问题,这里有一个快捷方式:您已经定义了“播放器”,并且可以在函数中使用它。祝您有美好的一天,愿上帝保佑这里的每一个人。

【讨论】:

  • 谢谢!它真的帮了我很多,非常感谢!
猜你喜欢
  • 2010-09-16
  • 2021-03-30
  • 2012-09-09
  • 1970-01-01
  • 2022-11-25
  • 2021-09-23
  • 2020-12-16
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多