Racing script troubles

And one more ?bug?, not critical but nevertheless - sometimes timer doesn’t count bumping + timer only blinks when car bumps into something, I want it to change color for about 5 sec. For example: if car stands right in front of rail and bumps into it - doesn’t work. Else - it works but not always. Maybe that is “HasEntityCollidedWithAnything” native’s promblem…

Hey there,
I’ve looked into the code but can’t really see which lines of code are not working properly. Could you name the functions (or the lines) where the problems occur? I don’t know the source code so can’t see what has been modified.

1 Like

Problem #1: 148-188 noclip check works perfectly, but I’m trying to apply check like this to the whole race (starts from 238 “While player is racing, do stuff”) and when I try to do this (line 264) whatever native I put there it just restarts the race even if I got “IsEntityVisibleToScript - true” or “IsPedInAnyVehicle - true”, “IsVehicleVisible” doesnt work tho and always prints me “false”. If I make another event like “raceNo” (line 206) all repeats.
Problem #2: Look at scoreboard on pic. 1, then look at scores.txt. There are 4 values: “minutes”, “seconds”, “ms” and “time”. They are all calculated in lines 268-272 and after this in lines 303-309 to receive a final score and send it to server. Somehow on pic. 1 people finish with more “ms” but in fact “time” in scores.cfg counted right and script compares it, not m:s:ms. I cant get how it compares it and how to make it work properly. (Don’t forget that I have that penalty time function in line 191 and it is calculated too).
Problem #3: This penalty time function doesn’t always count bumps (using “HasEntityCollidedWithAnything” native). Sometimes it does, sometimes doesn’t. Also timer blinks just for 1 sec when you bump into something, I want to make it red for like 5 sec but if I try to increase Citizen.Wait(0) for exaple, the whole script slows down and doesn’t work properly.

Ok, first of all in your codeshare the indenting is all messed up, can’t properly read it.
In order to find bugs I normally go forward and insert print('Test1'), print('Test2') and so on at various points during the code. You need to figure out how the code is running.

For your problem No. 1:
In the original script you find and if-clause that is used to reset everything at line 204. It would be line 249 for you.
In order to add a no-clip check you could basically copy and paste this and just change the if-statement.

Remember that in the raceCountdown-event you want the entity to be visible in order to trigger the race. Now during the race you want the script to act when your entity is NOT visible. I suspect that might be an issue here but can’t say for sure. As far as I’m concerned the only changes made to the raceActive-Event are 7 lines from line 241 on and possibly changes to the timer.

For problem No. 2:
I’ve not read properly the first time, the timings are messed up.
Now the problem here most likely does NOT lie with the sorting itself but rather with the displaying function. This is because the Lancer was faster than the Impreza, it’s just displaying wrong.
At the same time the ms-value for the Lancer-time is wrong, it should be 101, not 581.
Looking at your function racePenaltyTime I suspect that it’s the penalty time that is severely messing things up.
Please check:
Is the penalty time added at all times where it should be, especially when saving the timer for the end-result? I suspect with added penalties the Impreza is faster than the Lancer. Thus the displayed times should actually be correct, however the penalties wouldn’t be applied to the total (internal) time.

For problem No. 3:
Slightly related to Problem No. 2. Your problem here is that you have not correctly created the while-loop.
What you intend to do is:

while racing do
  Wait(0) --check for collisions every frame
  if CollisionDetected() then
    AddPenaltyTime()
    Wait(100) -- cooldown so the penalty time doesn't rack up hard
  end
end

What you are doing is:

while racing do
  Wait(0) -- check for collisions every frame
  if not CollisionDetected() then
    Wait(100) -- cooldown if you didn't find a collision.
    -- result: Check for collisions every 100 ms
  else
    AddPenaltyTime()
  end
end

Result: Fix your script regarding the penalty time first :smiley:
Also please do not use this function to color the timer red. Keep the variable raceState.RED however please do not use it in this function for now.
When you have fixed your collision system we will create a new event for this. Got an idea, will tell you when you are done :stuck_out_tongue:

1 Like

Starting with problem nubmer 1, look:
image
image


After countdown ends, it instantly just throws me outta race, even if I get “1”.

yeah, of course it throws you out. You want the vehicle to be visible, cancelling the event only if it is not. You got it the wrong way :smiley:

1 Like

Hm, still can’t understand what’s the point. If i put “1” instead of “false” - the same happens.
Ok, ok, let’s think logically, this native checks is vehicle visible to script. There are two if-statements, one for “if button pressed then restart”, another “if vehicle isn’t visible then restart”.
UPD: it works here


but doesn’t work inside “while player is racing do stuff” loop. I’m sitting like “wtf?” now.
As for the 3rd problem - I’ve fixed it, now it counts any “bump” correctly! Thanks for pointing my mistake out.

well, it seems you actually do not know the variable :smiley:
So, if-clauses work like this:

if A then print('Hi') will print Hi into the console if A is a correct statement. You surely know that. Consider the statement The height of the ceiling in your flat is 2.5 m.
It might be true, it might be false, I don’t know.
What I know is, let’s call h the height of your flat.

if h == 2.5 then
  -- this executes when the height of your ceiling is exactly 2.5 m
else
  -- this executes when the height of your ceiling is NOT 2.5 m
end

You can also use not as an operator.

if not h == 2.5 then
  -- this executes when the height of your ceiling is NOT 2.5 m
else
  -- this executes when the height of your ceiling is exactly 2.5 m
end

There are multiple ways to write not. h ~= 2.5 would be one way.

Now for your problem:
You are currently executing an if-clause with the statement IsEntityVisibleToScript which as you clearly states is true at the start of the race.
However you only want to cancel the race if the Entity is NOT visible, hence IsEntityVisibleToScript should be false.
Clear?

1 Like

Clear as the sky on a cold winter morning somewhere in Siberia. BUT. Could you explain, please, how is it relate to IsEntityVisibleToScript native? Native itselfs checks if a vehicle visible and returns 1 or false (prints above prove that). Or are you hinting that I need to make a new variable?

Nvm. I tried to do something like this


and it worked out…
UPD: I just can’t get HOW? It works with IsEntityVisibleToScript(GetVehiclePedIsIn(PlayerPedId(), 1)) in one place but doesn’t work in another :thinking: There’s an if in line 126 too :thinking:
UPD2: Or if in line 126 and if here - are different things?
UPD3: IsEntityVisibleToScript(GetVehiclePedIsIn(PlayerPedId(), 1)) checks if it returns 1, right? And IsEntityVisibleToScript(GetVehiclePedIsIn(PlayerPedId(), false)) checks if it returns false. The main question is why it doesn’t work in line 263 as I supposed?


Tried something on my own :grin: (red timer delay). Is it correct?

I’ve made the script add penalty time to finishTime when race is finished. Ms were displayed correctly but as it compares two finishTime variables and there wasn’t + raceState.penaltyTime, script counted that the Lancer was first (in fact it bumped several times and get those +500 extra ms, which were added to ms but not to finishTime, I guess)

No, that’s incorrect. Check the description of the native GetVehiclePedIsIn(). The 1 or false only will matter for peds entering or exiting a vehicle.
This should also hopefully clear up the question how the code works.
Btw:
if not IsEntityVisibleToScript(GetVehiclePedIsIn(PlayerPedId(), 1)) then would be a nicer line.

1 Like

Does it work? If yes it is a nice solution :smiley:
So now your code does the following:
It checks every frame for collisions, adds 99 ms of penalty time and pauses for 100 ms before resuming the check. The timer will display red for 3 seconds.
Please note that multiple collisions in a short time frame will still see the timer turn white after 3 seconds. E.g. you have a collision at t = 0 s and a collision at t = 1 s. The timer will turn white at t = 3 s, the second collision would demand it turn white at t = 4 s. But that shouldn’t be too much of a problem.

So the time comparing issue is fixed as well?

Yep! I see no problems with scoreboards on my server for now.

Works as intended, all credit goes to you of course :grin:
I can barely describe how I’m grateful for your help, time, all these detailed instructions and explanations… damn, I even started to think that you are a teacher or something like that (especially after 29th post).
Anyway, I’ve learned a lot of new things, tips and tricks during all this time while we were trying to figure out how to fix all this mess, so thanks again!

1 Like

@Silverman
It’s me again :sweat_smile:
Got 1 more problem: players can push each other during countdown saving 5-10 secs by that, kinda unfair to others. “SetEntityNoCollisionEntity” makes vehicle fall through the ground so I don’t know how to disable vehicle to vehicle collision only. Any tips?

Do you have a video for me showcasing the problem? Or do you mean players not in the race pushing racing players?

I’m searching for the way to disable collision only till countdown ends to prevent such things.