Stop the iteration if a condition is met?

I’m confused with my logic, probably because I’ve been doing this all day and I’m too tired, but this piece of code:

``````Citizen.CreateThread(function()
while true do
Citizen.Wait(60)

local hit, coords = ZMan.Utils.Game.RayCastGameplayCamera(7.0)

if hit and coords then
for k, v in pairs(targetList) do
if #(vector2(v.coords.x, v.coords.y) - vector2(coords.x, coords.y)) < 1.0 and showTarget == nil then
showTarget = v
elseif #(vector2(v.coords.x, v.coords.y) - vector2(coords.x, coords.y)) > 1.0 and showTarget ~= nil then
showTarget = nil
end
end
end
end
end)

while true do
Citizen.Wait(100)

if showTarget then
SendNuiMessage(json.encode({ type = "showTarget" }))
else
SendNuiMessage(json.encode({ type = "hideTarget" }))
end
end
end)
``````

So the `showTarget` variable keeps changing between `nil` and a `table` address, I know it’s because of my `for-loop` but I’m not too sure what I would need to do to fix it, but I need it to be true when we’re close to an object and skip the iteration or whatever, else it’ll go to the next if and make the variable `nil` , ah gosh I’m so tired

Did you try break? When the condition is met.

You should also take a break.
Good dev takes a break.

if its nil then try delcare the variable local yourvariable cause your viariable is inside thread it will not transfer to other thred it will become nill

and put only true or false if thats you need dont put nil

``````Citizen.CreateThread(function()
while true do
Citizen.Wait(60)

local hit, coords = ZMan.Utils.Game.RayCastGameplayCamera(7.0)

if hit and coords then
for k, v in pairs(targetList) do
if #(vector2(v.coords.x, v.coords.y) - vector2(coords.x, coords.y)) < 1.0 and showTarget == nil then
showTarget = v
break -- <-- stop/skip the for loop iteration (targetList)
-- or
-- while #(vector2(v.coords.x, v.coords.y) - vector2(coords.x, coords.y)) < 1.0 do
--     hit, coords = ZMan.Utils.Game.RayCastGameplayCamera(7.0)
--      Wait(100)
-- end
elseif #(vector2(v.coords.x, v.coords.y) - vector2(coords.x, coords.y)) > 1.0 and showTarget ~= nil then
showTarget = nil
end
end
end
end
end)
``````

@mjaltrinity
he probably insert

showTarget
as a global variable

The variable is `global` and making it nil or false will lead to the same exact result.

1 Like

Using `break` keyword, lead me to the same exact issue:

Everyone seems to understand my problem but you, if you look at your code you will know it is the exact same outcome as mine. Stop posting if you don’t know what you’re talking about and let the others try to help me.

Thanks and have a good day

you think so? haha okei sorry

Hey, try this:

``````local showTarget = nil

while true do
Citizen.Wait(60) -- are you sure this needs to be that fast?

local hit, coords = RayCastGamePlayCamera(7.0) --place yours

if showTarget == nil then
if hit and coords then
for k, v in pairs(targetList) do
if #(vector2(v.coords.x, v.coords.y) - vector2(coords.x, coords.y)) <= 1.0  then
showTarget = v
SendNuiMessage(json.encode({ type = "showTarget" }))
-- i guess there is no need to put this inside another loop?
end
end
end

else
if #(vector2(showTarget.coords.x, showTarget.coords.y) - vector2(coords.x, coords.y)) > 1.0  then
showTarget = nil
SendNuiMessage(json.encode({ type = "hideTarget" }))
end

end

print(showTarget)

end
end)

``````

Tried this code on my localhost and works fine

2 Likes

Well, you fixed it, thank you so much! And it even improved performance by a little, I’m disappointed in myself as I couldn’t figure out such simple issue, thanks!

1 Like