gpt4 book ai didi

for-loop - 如何使脚本影响 Roblox LUA 中的所有子项?

转载 作者:行者123 更新时间:2023-12-04 07:50:16 24 4
gpt4 key购买 nike

我是 LUA 编程的新手,虽然我已经学习了类似的语言,比如 JS。如果我必须通过替换每个脚本来更改组中许多部分的相同脚本,这令人沮丧,而且我不知道这样做的优雅方法。相反,我决定将所有部分嵌套在脚本中。我看过一些例子,我试图改编其中的一些,但它们并不完全适用于我想做的事情,我无法让它们发挥作用。
从本质上讲,我要做的是监控所有的砖块,以便玩家联系它们。我采用了嵌套在每个砖块中的原始消失砖块脚本并对其进行了修改。如果一个部分(砖)被触摸,那应该调用 onTouch 函数,这将使砖的透明度随着时间的推移而降低,直到成对循环完成,之后砖消失并且 CanCollide 被关闭。 2 秒后,它又恢复正常。我认为问题在于我用来监控部件的编码,因为我不太了解监控多个对象的正确方法。有人可以帮忙吗?谢谢!
文件结构:
File structure

function onTouched(brick)
local delay = .1 -- the delay between each increase in transparency (affects speed of disappearance)
local RestoreDelay = 2 -- delay before the brick reappears
local inc = .1 -- how much the brick disappears each time

-- All characters have a Humanoid object
-- if the model has one, it is a character
local h = script.Child:findFirstChild("Humanoid") -- Find Humanoids in whatever touched this
if (h ~=nil) then -- If there is a Humanoid then
h.Health = h.MaxHealth -- Set the health to maximum (full healing)
for x=0,1, inc do
script.Child.Transparency = x+inc
script.Child.CanCollide = true
wait(delay)
end
wait(delay)
script.Child.Transparency = 1
script.Child.CanCollide = false
wait(RestoreDelay)
script.Child.Transparency = 0
script.Child.CanCollide = true
else
end
end

while true do
local bricks=script:GetChildren():IsA("basic.part")
for x=1,brick in pairs(bricks) do
brick.Touched:connect(onTouched(brick)) -- Make it call onTouched when touched
end
end
end

最佳答案

在大多数情况下,您已经做对了,但是您遇到了一些语法错误,其中 JavaScript 和 Lua 之间存在不同的约定。
在 JS 中,您将获取一组对象,然后 bee 能够立即对其进行过滤,但在 Lua 中,对此的支持有限。所以像这样的一行:

var bricks = script.GetChildren().filter(function(item) 
return item === "basic.part"
end)
如果没有某个图书馆的帮助,就不能在 Lua 中一行完成。因此,当您遍历对象时,您需要将检查移到循环中。
除此之外,唯一需要更改的是 onTouched处理程序的函数签名。 BasePart.Touched事件告诉你哪个物体接触了砖块,而不是砖块本身。但是通过创建 higher order function ,很容易接触到砖块和接触它的东西。
-- create a helper function to access the brick and the thing that touched it
function createOnTouched(brick)
-- keep track whether the animation is running
local isFading = false

return function(otherPart)
-- do not do the animation again if it has already started
if isFading then
return
end

local delay = .1 -- the delay between each increase in transparency (affects speed of disappearance)
local restoreDelay = 2 -- delay before the brick reappears
local inc = .1 -- how much the brick disappears each time

-- All characters have a Humanoid object, check for one
local h = otherPart.Parent:FindFirstChild("Humanoid")
if h then
-- heal the player
h.Health = h.MaxHealth

-- start fading the brick
isFading = true
brick.CanCollide = true
for i = 0, 1, inc do
brick.Transparency = i
wait(delay)
end
-- turn off collision for the brick
wait(delay)
brick.Transparency = 1
brick.Anchored = true
brick.CanCollide = false

-- turn the part back on
wait(restoreDelay)
brick.Transparency = 0
brick.CanCollide = true

-- reset the animation flag
isFading = false
end
end
end

-- loop over the children and connect touch events
local bricks = script:GetChildren()
for i, brick in ipairs(bricks) do
if brick:IsA("BasePart") then
local onTouchedFunc = createOnTouched(brick)
brick.Touched:Connect(onTouchedFunc)
end
end

关于for-loop - 如何使脚本影响 Roblox LUA 中的所有子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67022609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com