Workspace.Gromaniak85.LocalScript:7: 预期 ')'(关闭第 25 列的 '('),得到 '.2xblock'

Workspace.Gromaniak85.LocalScript:7: Expected ')' (to close '(' at column 25), got '.2xblock'

我想要做的是获得从角色躯干到方块的距离。我不知道为什么,但是当我引用文件夹时,它给出了那个错误。

这是错误代码: 工作区。Gromaniak85.LocalScript:7:预期')'(关闭第25列的'('),得到'.2xblock'

local DistanceRemote = game:GetService("ReplicatedStorage").DistanceFromBlock
local player = script.Parent.Parent
local BrickMultipliers = game.Workspace.BrickMultipliers

while true do
    wait(0.5)
    local distancefrom2x = (game.Workspace[player.Name].UpperTorso.Position - BrickMultipliers.2xblock.Position).magnitude --error happens there
    print(distancefrom2x)
end

这是工作室里的样子:

https://i.stack.imgur.com/pTMar.png

我真的不知道该怎么做,所以我会感谢所有的帮助!!

您不能引用 Lua 中以符号(包括数字)开头的对象。您必须将其括在方括号和引号中。这意味着您还可以去掉点 (.):BrickMultipliers["2xblock"].Position 这将整数 (the 2) 与字符串的其余部分 (xblock) 组合在一起,从而使 Roblox 能够成功引用您的部分。

根据 Lua's lexical conventions 名称不得以数字开头!

Names (also called identifiers) in Lua can be any string of Latin letters, Arabic-Indic digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels.

The following keywords are reserved and cannot be used as names:

 and       break     do        else      elseif    end
 false     for       function  goto      if        in
 local     nil       not       or        repeat    return
 then      true      until     while

因此这是无效语法:BrickMultipliers.2xblock.Position

来自Lua 5.4 Reference Manual 3.2 - Variables:

The syntax var.Name is just syntactic sugar for var["Name"]:

var ::= prefixexp ‘.’ Name

所以这种方便的语法仅适用于 Lua 标识符(名称)。

由于 2xblock 以数字开头,因此不能是姓名。因此 BrickMultipliers.2xblock 导致错误。

对于任何不是 Lua 名称的 table 键,您必须使用方括号表示法。

BrickMultipliers["2xblock"]