How do I make a beam that follows the player mouse smoothly?

In the recent Power Simulator game, there is a power which allows the player to shoot a beam from their hands. This beams smoothly follows the mouse position and even a particle effect if emitted at the end of the beam. How is this achieved?

I have an idea where the server invokes the client for their mouse position and the server then creates the beam using the mouse position which was invokes, I can imagine this happens every wait() but wouldn’t that put strain on the server script?

Note that everyone in the game can see this beam so therefore isn’t a client only job. Below is a link to a gif: https://gyazo.com/3e0f3e6436883764c9fccdcd6a192532

3 Likes

My best guess at this would be a combination of ray casting and client to server at a fairly consistent rate. In your GIF I saw the ray to be delaying a little bit - enough to not be a wait() loop but not high enough that it wouldn’t seem smooth (don’t know if that was intentional or not), so I’d assume that the client would update the server with a position every .1 to .2 seconds or so.

I’m definitely not an expert in this area, so I don’t know how much help I can give you in this problem other than a general direction, good luck on finding the solution :sweat_smile:

1 Like

Oh I see, so you think Client to Server is probably a safer and efficient option rather than Server to Client, this actually makes sense, thanks for the contribution!

1 Like

For me it looks like the client sends every 0.1 seconds (or around that) the server the new mouse hit location, if you use TweenService with that and then tween the beam to the new CFrame you would achieve a smooth beam that would follow the players mouse.

Something like this: (or at least how I would do it)

Summary
  • Client sends every 0.1 seconds via a RemoteEvent a event to the server with the current mouse position
  • Server processes the new mouse position like this:
    – Get previous location and beam
    – Use TweenService to create a Tween to the new location
    – Play the Tween for 0.1 seconds
1 Like

Create a raycast on the server, fireallclients to visualize the beam, inside the client make the loop a renderstepped or heartbeat loop so it looks fairly smooth

1 Like

I see, it doesn’t have to be entirely smooth but have an impression of it, but does having it tween this often cause server stress?

@metaindex gave a good contribution, he practically modified my idea and said to send the client the new position so that the client processes the new tween and plays it, so that practically the server is the relay.

Yes I was going to mention, so something like this:

Client sends mouse position to server every 0.1 seconds > server deals with the raycasting and appropriate damage handling > server also uses :FireAllClients() in order for the clients to deal with the visual stuff.

I’m just worried that all this may put strain on the server side as I’ve read something about invocation queues, please correct me if I’m wrong.

You can also just change it to a lower send rate if you worry that much about server strain. The less requests, the less strain.

Ah I see, sorry if this becomes off topic and therefore you don’t have to answer, is there an area which I can look into to find out if scripts are causing too much strain?

The Developer Console shows you a lot of information, you should explore it a bit! To open it, go into one of your games and press F9 on your keyboard. I can promise it shows the script usage somewhere, but I can’t get the exact tab out of my mind right now.

Ah yes the good old dev console, I only ever use it to find bugs in a real server setting, I never explored further with the other tabs. Thanks!

1 Like