Roblox Studio中attempt to index number with 'Transparency'错误如何修复?
问题解决:螺旋桨透明度动画报错+代码优化
报错核心原因
你原代码里遍历GetChildren()返回的子对象时用了for c in p0,这是Lua的错误写法——GetChildren()返回的是数组型table,必须用ipairs遍历,直接写for c in table会触发迭代器错误,这就是你遇到问题的根源。
优化后的代码(简洁易维护)
不用手动定义30个变量,自动收集所有Propeller模型,用循环代替重复的代码块:
local p = script.Parent -- 自动收集所有Propeller模型的子部件,按序号排序 local propellers = {} for _, child in ipairs(p:GetChildren()) do local num = tonumber(child.Name:match("Propeller(%d+)")) if num then propellers[num + 1] = child:GetChildren() -- 转成从1开始的索引,方便循环衔接 end end local totalFrames = #propellers local stepDelay = 0.1 -- 动画间隔,可根据需求调整 while true do for current = 1, totalFrames do -- 计算三个状态的模型索引:完全显示、半透明、隐藏 local active = current local fade = current - 1 local hide = current - 2 -- 处理循环衔接(比如current=1时,fade对应最后一个模型) if fade < 1 then fade = totalFrames end if hide < 1 then hide = totalFrames + hide end -- 更新各模型的透明度 for _, part in ipairs(propellers[active]) do part.Transparency = 0 end for _, part in ipairs(propellers[fade]) do part.Transparency = 0.5 end for _, part in ipairs(propellers[hide]) do part.Transparency = 1 end wait(stepDelay) end end
更高效的替代方案
其实没必要创建30个模型切换透明度,直接用TweenService做旋转动画更省事,性能也更好:
local TweenService = game:GetService("TweenService") local propeller = script.Parent.Propeller -- 你的单个螺旋桨模型 -- 配置动画参数:0.2秒转一圈,无限循环,匀速旋转 local tweenInfo = TweenInfo.new( 0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, false ) -- 创建并播放旋转动画 local tween = TweenService:Create(propeller, tweenInfo, {Rotation = Vector3.new(0, 360, 0)}) tween:Play()
内容的提问来源于stack exchange,提问作者code-e-ghast
相关产品推荐
相关产品推荐

