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
. or
ing 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