For loop 如何使脚本影响Roblox LUA中的所有子脚本?
虽然我学过类似的语言,比如JS,但我对LUA编程还是新手。如果我必须通过替换每个脚本在一组中的多个部分修改同一个脚本,这是令人沮丧的,我不知道有什么优雅的方法可以做到这一点。相反,我决定将所有部分嵌套在脚本中。我已经看到了一些例子,我试着修改其中的一些,但它们并不完全适用于我想做的事情,我无法让它们发挥作用 本质上,我想做的是监视所有的砖块,让玩家联系它们。我取了嵌套在每个砖块中的原始消失砖块脚本,并对其进行了修改。如果触摸到一个零件(砖块),则应调用onTouch函数,该函数将使砖块的透明度随着时间的推移而降低,直到完成in pairs循环,之后砖块消失,CanCollide关闭。2秒后,它会恢复正常。我认为问题在于我用来监视部件的编码,因为我不太了解监视多个对象的正确方法。有人能帮忙吗?谢谢 文件结构:For loop 如何使脚本影响Roblox LUA中的所有子脚本?,for-loop,lua,touch-event,children,roblox,For Loop,Lua,Touch Event,Children,Roblox,虽然我学过类似的语言,比如JS,但我对LUA编程还是新手。如果我必须通过替换每个脚本在一组中的多个部分修改同一个脚本,这是令人沮丧的,我不知道有什么优雅的方法可以做到这一点。相反,我决定将所有部分嵌套在脚本中。我已经看到了一些例子,我试着修改其中的一些,但它们并不完全适用于我想做的事情,我无法让它们发挥作用 本质上,我想做的是监视所有的砖块,让玩家联系它们。我取了嵌套在每个砖块中的原始消失砖块脚本,并对其进行了修改。如果触摸到一个零件(砖块),则应调用onTouch函数,该函数将使砖块的透明度随
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中,您将获取一个对象数组,然后能够立即对其进行过滤,但在Lua中,对它的支持有限。这样的一句话:
var bricks = script.GetChildren().filter(function(item)
return item === "basic.part"
end)
如果没有某些库的帮助,无法在Lua中的一行中完成所有操作。因此,在遍历对象时,需要将检查移动到循环中
除此之外,唯一需要更改的是ontoched
处理程序的函数签名。事件告诉您哪个对象接触了砖块,而不是砖块本身。但是,通过创建一个新的界面,很容易接触到砖块,以及接触到砖块的东西
-- 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
哇,谢谢!我一直在考虑使用IsA,但不确定它是否有用。我最初使用常规for循环,而不是成对使用。仍然不能完全确定何时使用常规vs成对vs iPair。你能解释一下吗?它是否与IPAIR能处理哪些配对不能处理有关?@GAMII
IPAIR
表示“数组”:当表的(有趣的)键是从1
开始计数的数字时。通常在lua中,表可以用数字(如数组)索引,也可以用任何其他类型的对象(如字典或关联数组)索引。千万不要把这两种东西混在一张桌子上,否则你会得到疯狂的结果。当您使用ipairs()
时,您的意思是希望表像数组一样工作pairs()
通常更灵活,可用于数组或字典,但它有助于可读性。它并不真正用于堆栈溢出,因此我不确定@Kylaaa或aschepler是否为我提供了解决方案…:(好像是Kylaaa,aschepler添加了关于角色部分的注释。后者让我问角色的某些部分是否需要GetDescents?另外,函数(其他部分)是系统函数吗?我剪切并粘贴了您给我的内容,但它不起作用(当您连接到toucted
事件时,它调用如下函数:brick.toucted:connect(函数(其他部分))
。我刚刚将该功能移动到了一个可以访问原始砖块的位置……嗯。您可能会遇到角色模型多次接触零件的问题,这可能会影响淡入淡出动画。可能需要进行一些去抖动。