"attempt to index function with 'Connect'" 在 Roblox 中是什么意思?

What does "attempt to index function with 'Connect'" mean in Roblox?

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)

我正在尝试编写代码以在您单击时提供强度。当我按 'Play' 它不起作用。它在输出中所说的是 '尝试使用 'Connect' 索引函数。'

错误“attempt to index [type] with [field name]”意味着您错误地使用了对象之类的东西,并且您正在尝试访问其中的某些字段。

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

自从您处理 Tools, there is a simple fix. Instead of using the Activate function, you are looking for the Activated 事件以来。所以只需像这样更改更新该行:

script.Parent.Activated:Connect(function()

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

local tool = script.Parent

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

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