Is there a way to make my mouse steering system more efficient?

Hi all!
For the past month or so, I’ve been wanting to make a vehicle simulator which features mouse steering. It’s a fun and realistic way to drive your vehicles, so I want to implement it in my vehicle simulator game. I have more than a few issues though, and I’d be thankful if someone could help me out.

The first way I thought of making the vehicle steering is to get the mouse position, detect its position in the screen, and however far it is from the center of the screen divide it by three, and use bodygyros to rotate the vehicle. This is how I made my plane script, but the way I did it is by creating 2 remote events (one for the handling and one for the throttle) and sending them every 0.3 seconds, so that the main aircraft script could get the mouse position and do the math. I find this very inefficient and something I wouldn’t want to have in my vehicle steering script, but it does work.

wait(0.05)
	if value == true then
		PlaneEvent:FireServer(mouse.Hit.Position,value,(Vector2.new(mouse.X, mouse.Y) - screenCenter))
	else
	end

My question is: is there a better way to get the mouse position instead of using remote events and a loop?? Could there be some other way to make the mouse steering??

I’m extremely sorry if there’s another post with this same topic, I couldn’t find anything relevant. Please do tell me if I posted this in the wrong category.
Thank you!!

You should use the Heartbeat or Renderstepped event from the RunService service.

More info: RunService | Documentation - Roblox Creator Hub

1 Like

Thank you for the reply! I appreciate it. However, if I use RunService for that, I’d need a RemoteEvent i’m pretty sure.
I’m not sure though if I could directly, from the localscript, apply the mouse position value to the vehicle. Would it rotate with the mouse? Would nothing happen?

Instead of using wait(0.05) you should use the RunService as the code snippet you provided is on the client because you use the :FireServer method. For example:

RunService.Heartbeat:Connect(function()
	if value == true then
		PlaneEvent:FireServer(mouse.Hit.Position,value,(Vector2.new(mouse.X, mouse.Y) - screenCenter))
	else
end)

Why would you need a remote event for RunService? if you want to apply the mouse position value to the vehicle you should get the mouse position on the client and send it to the server through a remote event to set the position, you shouldn’t set it on the client unless you have a different goal to what im thinking.

I’m not really sure, but could this cause lag to the server and/or client? It’s what makes me worried the most. Would I need to use more than one remote event? What about the monitor size, would that make some vehicles steer more than others??

RunService is more efficient that a while true loop as it syncs with the clients frame rate or heartbeat rather than a set interval between each code block. Most of your scripts that require loops should be handled by these events not while true loops.

Why do you think you need to use more than one remote event, what are you trying to do? It shouldn’t effect the vehicles steer from the monitor size, although you could make restraints on the server before you steer the vehicle just in case.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.