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)
Citizen.CreateThread(function()
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
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
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)
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.
local showTarget = nil
Citizen.CreateThread(function()
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)
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!