Racing script troubles

Hi,
I’ve downloaded this racing script for my fivem server, made several changes but can’t figure out how to make last really important one. In general, I want race timer speeds up for a bit when a player bumps into something. Any ideas how to make this possible? Also found a glitch which allows to start a race on foot or using noclip so I want to prevent these from happening.

1 Like

Hey there,

you’ll need to figure out many things yourself however here’s the general idea for your “bumping”-problem:

This native is your friend. You want to utilize this in a new thread, that gets created whenever the race is running.
From scanning the code, a player is racing when raceState.index ~= 0.
So:

Create a new event that triggers whenever a player starts a race (Check lines 126 or the event raceCountdown to find a proper place).
That event should have the while loop mentioned above. The while-loop should run as long as the race is on, terminating as soon as the player stops racing.

An alternative to this would be using the game event CEventVehicleCollision although I don’t exactly know how it works → Research.
https://docs.fivem.net/docs/game-references/game-events/
https://docs.fivem.net/docs/scripting-reference/events/list/gameEventTriggered/

If you have successfully determined when a player collides with another player or the racetrack, you need to add time to his race. I’d suggest adding a flat time penalty, e.g. 1 second, to his race whenever he is hit (I’m not sure whether accelerating the GameTimer is possible or a wise idea).
To do that add a variable in the raceState-table called penaltyTime.
Utilize the while-loop you created above: Whenever a player collides with somebody, add one second to penaltyTime.
This needs to be reflected in the printed timings. Simply add the penalty time to the GameTimer (beware: Most likely the gametimer runs in milliseconds instead of seconds). You’ll surely need to do it in lines 220 and 235, I might have missed lines here.

Have fun figuring out that stuff. If you have zero experience coding, feel free to ask for more details. I won’t write you the code though… :stuck_out_tongue:
As a side-note which you might encounter during coding:
You might see the timer jumping up rapidly as no collision between entities is instantaneous. In this case you might wanna consider adding a cooldown to the collision detection (simply: Wait(2000) for a 2 second cooldown; care: the originally created “Wait” should always be a Wait(0) as the linked native needs it. The Wait mentioned here would be an additional one).

It might even be possible to have the text blink in red when a time penalty is added. That would be a fun project :wink:


Edith:
No idea how to prevent a race from starting when noclipping. However:

Check whether this native returns true before allowing somebody to start a race. This will force players to be in any vehicle (possibly also planes etc idk) before starting the race. You’ll need to add this in an if-clause at around line 125 (possibly exactly that line).

1 Like

Firstly, wanted to thank you for such detailed response, I really appreciate it. Never expected to get something like this in my topic :grin:
And yes, I have almost zero experience coding but always glad to improve my skills step by step. Anyway, that’s not what this about now.
As for your solutions, I’ve managed to apply is_ped_in_any_vehicle and it works! “Bumping” will be the last thing in my to-do list because of its complexity :sweat_smile:
Now I’m trying to figure out how is_vehicle_visible works to prevent noclip usage b4 race. There are 2 parameters (Vehicle vehicle) and I suppose that I should get vehicle model or something. Declaring local var gives me nothing.


Am I’m doing something wrong or is_vehicle_visible is not for stuff like this?

I don’t know whether you can make it work using IsVehicleVisible however quite a few corrections:

a)
There’s only one parameter, you see Vehicle vehicle but the last one is a comment.
b)
Don’t put in the model, put it the vehicle itself. E.g. CreateVehicle - Natives @ Cfx.re Docs specifically mentions ModelHash --[[ hash ]] which would stand for the hash of a model.

One tip: If you want to figure out which natives can work to detect noclip:

CreateThread(function()
  while true do
    Wait(1000)
    -- put everything you want to test in here, e.g.:
    print(IsVehicleVisible(GetVehiclePedIsIn(PlayerPedId())))
  end
end)

This is obviously only testing code and you should delete it after testing however can accelerate quite a bit.

Also nice job figuring out the first things and expanding yourself. I was intentionally a bit vague describing a possible solution (by no means the best so if anybody spots mistakes or easier ways feel free to point that out). Can go into more detail if necessary but only after you try :stuck_out_tongue:

1 Like

Good news, man! I found one native which returns “1” (w/o noclip) and “false” (with noclip on)!


Oh god, thanks to your script, it would’ve taken much longer w/o it… The last step is to decide how I’m gonna restrict noclip during race:
a) disable noclip button for an entire race
b) put this native in a loop
I assume that looping will affect overall performance, correct me if not :thinking:

Yes, every loop will affect performance.
As for optimization:
Try to code as little loops as you can. Think carefully about the loops you need to code:
Every while will have a Wait(x) in it. The higher you choose x the fewer times it will get executed.
So:
The collision-native is checked on tick - if looped do it on Wait(0) else you’ll not get it to work properly.

Here:
You can disable noclip for the race if you are certain that no one is going to use external cheats or the likes.
If you want to use the native, my goto would be putting the native in a loop which runs on most likely a Wait(1000).

Disclaimer, I have exactly zero experience with anti-cheat and thus no clue if what I’m writing is at all sensible :smiley:

1 Like

I moved on “bumping” problem. Well… It kinda works but it not, unfortunately (I know I made a mistake somewhere, point me where, please :grimacing:)

  1. Added penaltyTime variable in the raceState-table
  2. Made a loop (probably wrong)
  3. Timer disappears for a sec when I bump into something (it actually adds time to it)

well, you need to post the whole sourcecode :smiley: best post it in the coding environment.

1 Like

however just looking at the posted picture:

a while-loop executes a certain aspect whenever a condition is correct.
Your condition in this case is HasEntityCollidedWithAnything, so it’s sorta correct but not really.

What you want here is

while --[[is race currently active]] do
  wait(0)
  if HasEntityCollided... then

end

Don’t copy+paste this, it is not at all complete code. Refer to my first answer regarding where to place it. This is what I meant with a While-loop for collisions.

1 Like

Got it. One question - there is while raceState.index ~= 0 do already so I’m at a loss what should I exactly do. I mean, is it ok if I make another loop with raceState.index ~= 0?

Yes. You should think about when to trigger it though. Create a game event for it which you can trigger when you start a race.

1 Like

For example, I put it there (without creating game event first).


It gives me “error parsing script” :frowning:

Is it telling you where the error is?
Because you are not ending the if-clause with end :smiley:

1 Like

Goddamn, I couldn’t even imagine that I should put end after if, my bad :rofl:

Look, if I put it there, timer shows for a sec and then disappears completely.


And if I put it there, it doesn’t even appear :thinking:

Should I find another place for this or still create a new event?
upd: it breaks the whole race (can’t collect checkpoints etc.), I suppose that incorrect incrementation is a problem :thinking:

1 Like

You still need to create a completely new event ^^

1 Like

YES, IT WORKS HAHAHAHA! This night was well spent, check this out: YouTube
I can’t even express how grateful I’m to you! Huge respect for all this help :hearts:
Can I dm you in case of further questions about modding? :pleading_face: :point_right: :point_left:

Well, now you might want to check how much time you add for a collision. It’s a little less than a minute!
Kinda too much but if you insist… :smiley:
Nice job! Can you once more post the full source code (ideally not in a pastebin but directly to the forum)? Will be helpful if someone else has a similiar question.

EDIT: DMs are fine but forum topics are preferred. That way more people can answer ^^

1 Like

Sure, I’ll do it as soon as I can (there are things need to be fixed/optimized).
As for the time… well, I think 50000ms is ok for penalty :rofl:

@Silverman
Hi! Sorry for disturbing you again. Yes, I still work on this timetrials script and have a big promblem, really big one. As you can see, I’m trying to prevent nocliping during race. I’ve made a check while countdown starts and it works perfectly - if player uses noclip the race restarts.


BUT in the code below I can’t make it work and don’t really understand why. Take a look, it’s kinda messy but my eye is already twitching cz I’ve been trying for about 8 hours and nothing works:

“Debug mode” prints me that
A) IsVehicleVisible (false)
B) IsPedInAnyVehicle (“1” that is true)
C) IsEntityVisibleToScript (player’s entity, not vehicle, “1” = true too)
If I try to put these natives in the code, as soon as the timer expires, the race restarts like noclip is “on” but in console I’m getting opposite (A, B, C values).
I also tried to put it inside an event like I did on 1st pic, make more vars, use another natives - the same result. It’s really strange that whatever value is assigned it always act like if it’d get false.
And got 1 more problem…
image
image
image
Somehow it counts miliseconds wrong or idk
If I understand right - it compares only “time” var but why ms are counted like this then