为什么属性是“名称”?在 Roblox 中,“名称”并不存在。就像“类名”一样在代码中

发布于 2025-01-14 07:03:03 字数 736 浏览 5 评论 0原文

我在 Roblox api 中看到了代码示例,类“Name”未用作“Name" 代码如“ClassName" 即可。你能告诉我原因吗?

例子: (类名代码)

 for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.ClassName == "Part" then
        print("Found a Part")
        -- will find Parts in model, but NOT TrussParts, WedgeParts, etc
    end
end

(名称代码)

    local baseplate = workspace.Baseplate
local baseplate = workspace["Baseplate"]
local baseplate = workspace:FindFirstChild("BasePlate")

I saw code example in Roblox api and class "Name" wasn't used as "Name" in code like "ClassName" do. Can you tell me the reason?

example:
(ClassName code)

 for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.ClassName == "Part" then
        print("Found a Part")
        -- will find Parts in model, but NOT TrussParts, WedgeParts, etc
    end
end

(Name code)

    local baseplate = workspace.Baseplate
local baseplate = workspace["Baseplate"]
local baseplate = workspace:FindFirstChild("BasePlate")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2025-01-21 07:03:03

好吧,如果有意义的话,ClassName 基本上就像一个“类型”。名称是为了缩小该部分的实际NAME范围。假设我有一个如下所示的工作区目录:

Workspace:

  • CoolPart

  • 另一个部分

如果我这样做:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.ClassName == "Part" then
        print("Found a Part")
    end
end

它将打印“找到零件”两次,因为 CoolPart 和 AnotherPart 都是工作区的零件(不是桁架,不是楔子,而是实际的砖块零件),但如果您这样做:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.Name == "Part" then
        print("Found a Part")
    end
end

它不会打印任何内容,因为工作区中的所有内容实际上都没有标题为“部分”,您明白我的意思吗?

Alright, so a ClassName is basically like a 'Type' if that makes sense. Name is to narrow down the actual NAME of the part. So lets say I have a workspace directory that looks like this:

Workspace:

  • CoolPart

  • AnotherPart

If I do:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.ClassName == "Part" then
        print("Found a Part")
    end
end

It will print 'Found a Part' twice, because both CoolPart and AnotherPart are a Part (not a truss, not a wedge, an actual brick part) of the workspace, but if you do:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child.Name == "Part" then
        print("Found a Part")
    end
end

It won't print anything, because of everything that is in workspace, none of them are actually titled 'Part', you get what I mean?

皓月长歌 2025-01-21 07:03:03

桁架零件与零件不同,因为球只是形状属性发生变化的零件。这与桁架零件不同。桁架零件虽然是基础零件,但不是零件。如果您确实想做您所做的事情,请使用:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child:IsA("BasePart) then
        print("Found a Part")
    end
end

您还应该使用 if Instance:IsA("BasePart") then... 因为它会提示/显示您可以使用的内容。

A truss part is different from a part as a ball is just a part with it's shape property changed. This is NOT the same for a truss part. A truss part, although a base part, is not a part. If you do want to do what you did, use:

for _, child in ipairs(game.Workspace:GetChildren()) do
    if child:IsA("BasePart) then
        print("Found a Part")
    end
end

You should also probably use if Instance:IsA("BasePart") then... because it prompts / shows what you could use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文