好吧,我只是好奇如何提高效率。我知道它可能与诸如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
您可以使用 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