有没有一种方法可以使组中的每个部分变量(roblox.lua)?

发布于 2025-01-29 05:45:21 字数 253 浏览 1 评论 0原文

因此,我最近制作了一个Roblox脚本(在LUA中),该脚本在按下按钮时会更改我想要更改的对象的颜色(灯),并使用“代码1”“代码2”弹出GUI等等。每个“代码”都会改变光所能做到的东西。现在,出于我的问题,我只制造了一个光,即光线效果很好,但是我希望到处都是光,每个灯都必须能够通过脚本更改。因此,我有1盏灯,但是我想制作数十个灯光,并将它们放在设施中的任何地方……有没有办法使每种光的变量变量,否则我将不得不进行太多的变量和线条。如果您需要脚本的一部分,请告诉我,灯光或位置都在。 谢谢。

So I recently made a Roblox script (in lua) that powers when a button is pressed, it will change the colors of the object I want to change (a light) and it pops up a gui with “code 1” “code 2” etc. every ‘code’ changes the stuff that the light will do. Now, for my problem, I have made only one light, that light works well and stuff, but I want this light everywhere, and every light has to be able to be changed by the script. So I have 1 light, but I want to make dozens of lights and place them everywhere in the facility… is there a way to make a variable of every light, otherwise I will have to make too much variables and lines. If you need a part of the script, the light or the position both are in, tell me.
Thanks.

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

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

发布评论

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

评论(1

剑心龙吟 2025-02-05 05:45:21

当我需要在许多不同的地方需要特定复杂模型时,我有时采用的一种一般方法:

我只是在希望模型的地方创建简单的占位符部分,而不是复制和粘贴模型。我放入一个文件夹中的所有这些零件。然后,我有一个脚本,启动时将用我想要的模型的克隆代替所有这些占位符零件。这是一个例子:

  • 在游戏中的每个地方放置一个零件。
  • 在工作区中创建一个文件夹“ mylights”,然后将所有这些零件放入该文件夹。
  • 给零件独特的有意义的名称,因为您可以在以后通过这些名称访问它们。
  • 在ServerStorage中创建一个文件夹“资产”。
  • 完成建模后,创建您想要的光的模型,并将其放入该资产文件夹中(请确保为模型设置主要部分)。

现在,在Mylights文件夹中创建以下脚本:

local lightsFolder = script.Parent
for i,v in pairs(lightsFolder:GetChildren()) do
    if v:IsA('Part') then
        local asset = game.ServerStorage.Assets.Light:clone()
        asset:SetPrimaryPartCFrame(v.CFrame)
        asset.Name = v.Name
        asset.Parent = lightsFolder
        v:destroy()             
    end
end

因此,在启动时,您的灯光将被放置在这些部分的位置。现在,您只需要在一个地方修改光的外观和行为。如果您不希望它们看起来都一样,则可以在占位符中定义属性并脚本将其应用于克隆操作。我给你一个例子:

假设您希望主要部分是红色的,但仅在一个灯中。选择占位符部分,然后在属性编辑器中转到属性部分,然后单击“添加属性”。在“添加属性”对话框窗口中,“ color”为名称,“ color3”作为类型,然后单击“确定”。添加了属性“颜色”。为其选择红色。

现在,将以下行添加到上面的脚本中,就在C:destion()行之前:

    local color = v:GetAttribute("Color")
    if color ~= nil then
        asset.PrimaryPart.Color = color 
    end

您将看到您在占位符属性中设置的颜色仅应用于放置在其位置的模型中。

所有这些都是本着保持程序干燥的精神。

A general approach I sometimes take when I need a particular complex model in many different places:

Instead of copying and pasting the model around, I just create simple placeholder parts where I want the models to be. All those parts I put into one folder. Then I have a script that on startup will replace all those placeholder parts with a clone of the model that I want in their place. Here is an example:

  • Put a part at every place in the game where you want a light.
  • Create a folder "MyLights" in workspace and put all those parts into that folder.
  • Give the parts unique meaningful names, as you can access them through those names later on.
  • Create a folder "Assets" in ServerStorage.
  • Create a model of the light you want and put it into that Assets folder when you're done modelling it (make sure to set a primary part for the model).

Now create the following script in the MyLights folder:

local lightsFolder = script.Parent
for i,v in pairs(lightsFolder:GetChildren()) do
    if v:IsA('Part') then
        local asset = game.ServerStorage.Assets.Light:clone()
        asset:SetPrimaryPartCFrame(v.CFrame)
        asset.Name = v.Name
        asset.Parent = lightsFolder
        v:destroy()             
    end
end

So upon startup your lights will be placed where ever those parts are. You now only need to modify the looks and behavior of the light in one place. If you don't want them to look all the same, you can define attributes in the placeholder and script them to be applied in the cloning operation. I'll give you an example:

Let's say you want the primary part to be red but only in one of the lights. Select the placeholder part, and in the property editor go to the Attributes section and click "Add Attribute". In the Add Attribute dialog window enter "Color" as Name and "Color3" as Type and click OK. An attribute "Color" has been added. Choose a red color for it.

Now add the following lines into the above script, just before the c:destroy() line:

    local color = v:GetAttribute("Color")
    if color ~= nil then
        asset.PrimaryPart.Color = color 
    end

You will see that the color you have set in the placeholder's attribute is only applied to the model that is put in its place.

All this is in the spirit of keeping your program DRY.

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