[HELP] Trying to make Current Plate / Last seen plate

How come this doesn’t work?

Basically if I have something in front me it works (not nil) but when I move away from the vehicle in front of me the other section of the code doesn’t work =/

						if boatTarget ~= nil then
						temp = GetVehicleNumberPlateText(boatTarget)
						current = temp
						drawTxtPolice(current,0,1,0.878,0.53,0.5,255,255,255,255)
						drawTxtLast(old,0,1,0.878,0.58,0.5,255,255,255,255)
						else
						drawTxtPolice('~g~None ~s~available',0,1,0.86,0.53,0.5,255,255,255,255)
						drawTxtLast(old,0,1,0.878,0.58,0.5,255,255,255,255)
						end
						old = current

changed OP please someone if you know what I am doing wrong =/

does anyone know why the code doesn’t work? (the else part)

It means that boatTarget is never nil
Try to print boatTarget and detect if this value is updated

Show us how you detect BoatTarget, if you use ray casting, it will only work if the vehicle is in front of your ped.

I am not at home right now, but basically its the same raycast @Bob_74 linked in another thread for speedcamera…

so boatTarget = functionforraycast()

but my problem is that, shouldn’t the else work in here when there is nothing in front of the player? It works when there is a boat in front, but when you move away it disapear and it doesnt show “none available” or last seen =/

if boatTarget ~= nil then
– do this
ELSE
o this while nothing in front of player
end

the else part never works at nil or not nil =/

Without the full code, we won’t be able to help you much.
But, I think you can simply use Citizen.Trace() to ensure you go through your “else” (from what I see in your code, your “else” should be good)

I’ve used the whole script you posted earlier and there is definitely a weird problem with the if boatTarget ~= nil then line.

I’ve noticed the following.
When the raycast doesn’t hit a boat:

  • boatTarget ~= nil is TRUE whereas we expect it to be false (no entity has been detected so boatTarget should be nil!)
  • Citizen.Trace(boatTarget) => BAM! Script error because (according to the console) it’s trying to show a nil value
    So we have two different behaviours for the same value tested differently (with ~= nil test and with Citizen.Trace() internal checking) :confused:

The workaround I’ve found is to replace

if boatTarget ~= nil then

with

if boatTarget > 0 then

It’s a quick and easy fix, but maybe we should investigate why does the script allow a supposedly nil value to pass the test.

Hope it will help you!

1 Like

Thank you very much for your time and help! it did indeed work using > 0 instead of nil, however, I removed my post easier because I took sort of “fix” it haha

What I did is simple, since no matter what I did I couldn’t get the else to work for the ~= nil statement, I just make another if expression prior to the ~= nil one.

So basically I have now:

if entityexist and isvehicle (both testing boatTarget) then
I do the ~= nil here
end

before ending the new test I add an else that display “none available” and the last seen plate, it works too :slight_smile: