Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

lua - I want it to loop only once

I have a problem with looping. I want it to loop only once, but it prints constantly. If I do return, the script will stop, i don't want that, because i would have to restart server always the script would be triggered. Any idea how to solve this problem?

    local hit = GetPedLastDamageBone(ped)
    local chance = math.random(0,10) 
    local ped = PlayerPedId()
    while true do 
        Wait(0)
        if GetEntityHealth(ped) > 0 then
            if DMGByWeapon then
                if hit == 45454 or 33646 then
                    print("Foot")
                end
            end
        end
    end
question from:https://stackoverflow.com/questions/65898081/i-want-it-to-loop-only-once

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I want it to loop only once

The whole purpose of a loop is to execute a block of code multiple times.

So if you only want to loop once, you don't need a loop. Just remove the loop from your code.

local hit = GetPedLastDamageBone(ped)
local chance = math.random(0,10) 
local ped = PlayerPedId()
Wait(0)
if GetEntityHealth(ped) > 0 then
  if DMGByWeapon then
    if hit == 45454 or 33646 then
      print("Foot")
    end
  end
end

but it prints constantly.

while true do end

is an infinite loop. The condition is always true and will never change so the loop will run forever.

If I do return, the script will stop

Your script file is compiled as a chunk. Chunks are treated as the body of an anonymous function. return will terminate that function and hence your script unless you use it inside a function definiton in that chunk.

If you want to stop a loop prematurely use break

Please read the Lua manual and do a beginners tutorial.

if hit == 45454 or 33646 then
   print("Foot")
end

Is eqivalent to

if hit == 45454 or true then
   print("Foot")
end

which simplifies to

if true then
  print("Foot")
end

or simply

print("Foot")

Any value that is neither nil nor false is true So 33646 is logically true. oring anything with true yields true

An if statement with a condition that is always met is pointless.

Instead of nesting several if statements you can simply combine the conditions using logical operators.

So instead of

if GetEntityHealth(ped) > 0 then
  if DMGByWeapon then
    if hit == 45454 or 33646 then
      print("Foot")
    end
  end
end

You could write

if GetEntityHealth(ped) > 0 and DMGByWeapon then
  print("Foot")
end

But if you really want to use that if statement you need to fix the condition:

if hit == 45454 or hit == 33646 then
  print("Foot")
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...