Best way to make a laps script?

Hey guys, I’m making a racing game and so far so good except I need to make a lap counter and I don’t know what methods I can use. I don’t want to use a .touched script for checkpoints because I find that sort of unreliable in a live server. Any ideas? Thanks,
-big

3 Likes

You could use Region3s and checkpoints. It should be far more reliable than touched.

5 Likes

I’d use a combination of region3s and raycasting. You could have a region3 check every so often. The region would be a bit larger than the checkpoint/finish line so that if there is any lag it will still be reliable.

while wait(1) do
    — if car parts are in region3 then start raycasting
    — else stop raycasting
end

The raycasting would basically act as a tripwire that extends the whole width of the checkpoint. If a car part goes through the raycast then you know who hit the finish line first.

2 Likes

Could also run checkpoints with a touched script running inside the character. A little more reliable than running touched on the server from my previous testing.

1 Like

Can confirm. I used this for something other than racing a few days ago and was astonished at how much more reliable touched was in the character instead of on some random part in the server.

1 Like

I think because the character is more closely connected to being ClientSided than ServerSided? Idk. Literally touched inside the Torso of a character seems better than touched inside a vehicleseat. Unsure why, but just how it seems at the very least.

2 Likes

I’ll probably end up doing regions now that you mention it - as for @Dollar500’s idea though, I like a tripwire but I don’t know if a raycast is the best way to do it; it seems costly to performance…?

1 Like

Depends. If you do use raycasting, it seems that shorter rays with no ignore lists are better for perfomance.

1 Like

Best method imo, is to do client sided region3 checks periodically, and then only send to server when you change regions.

On server, you should do a verification region3 check. This greatly reduces the number of region3 calls you make on server, while distributing the load to the clients.

1 Like

Region3’s are WAY more expensive than raycasts

Based on what? This is an irresponsible statement without further details. A long raycast with a huge ignore list can be more expensive than a Region3 check.

Always measure performance in the specific situation that you are using these API members in. Region3s and raycasts do not have a fixed cost, it depends on what you feed to them as parameters and the distance/area in which you look for hits.

1 Like

This is where I had gotten that info:

The reason I said “way more expensive” is because, with the method I posted, he would need to do raycasting/region3 checks every 0.1 seconds or so in order for the check to be as accurate as possible. Based on what AllYourBlox stated in that post, I would assume doing a region3 check that many times per second would be pretty expensive…

[Edit]: I shouldn’t have said that anyways since I didn’t test it and OP has a different scenario.

I think I’ll end up doing this:

  • I’ll convert the “part” checkpoints to Region3’s (in a script so my partner doesn’t need to do the math)
  • Client checks if vehicleSeat is within Region3
  • If they are, it fires the server with tick() as an argument
  • Server will verify region3 when fired
  • if the checkpoint is the start/finish line, it’ll check the tick() of each car that crossed the line and sort the racers based on time.

The only problem would be that the tick() could be manipulated… hmmm

Why not just do tick() on the server?
Otherwise, you completely leave the timing to individual players.

I’m probably going to end up doing this but it leaves a window open for the possibility of network lag because if car A with a slow connection crosses the finish line first, then car B with a fast connection crosses the line, if car B fires the server first because it has a faster network connection than car A, then car A will be placed after car B. Though I guess security needs to come first nonetheless. Not to mention, tick() is a local value so the small variation would make it impossible to use across the server & clients.

If you really wanted to, you can approximate vehicle velocities on the server to judge the winning player.