LuaU - 需要提高效率,不想发送垃圾邮件 elseifs

问题描述 投票:0回答:1

好吧,我只是好奇如何提高效率。我知道它可能与诸如pairs()之类的表循环有关。我知道这是糟糕的代码,我想找到一种方法让它不那么糟糕。

if string.find(subject.Name, 0) then
    inVal.Value = 0
elseif string.find(subject.Name, 1) then
    inVal.Value = 1
elseif string.find(subject.Name, 2) then
    inVal.Value = 2
elseif string.find(subject.Name, 3) then
    inVal.Value = 3
elseif string.find(subject.Name, 4) then
    inVal.Value = 4
elseif string.find(subject.Name, 5) then
    inVal.Value = 5
elseif string.find(subject.Name, 6) then
    inVal.Value = 6
elseif string.find(subject.Name, 7) then
    inVal.Value = 7
elseif string.find(subject.Name, 8) then
    inVal.Value = 8
elseif string.find(subject.Name, 9) then
    inVal.Value = 9
end
lua luau roblox-studio
1个回答
0
投票

您可以使用 for 循环,然后使用循环的索引将代码减少到只有六行。

在下面的脚本中,我们迭代

subject.Name
并查看索引(一个数字)是否在
subject.Name
中,然后设置
inVal.Value
如果匹配。

for index: number = 0, 9 do
    if string.find(subject.Name, tostring(index)) then
        inVal.Value = index
        break
    end
end
© www.soinside.com 2019 - 2024. All rights reserved.