What do you want to achieve? I’m trying to create a system that translates the User’s movement to the movement of two perpendicular lasers.
What is the issue? My current way of implementing this seems and is highly inefficient, however I can’t think of anyway else.
What solutions have you tried so far? I havent been able to find anything that effrctively solves my problem. Heres my code:
local HoldingW = false
local HoldingA = false
local HoldingS = false
local HoldingD = false
UIS.InputBegan:Connect(function(input, gameProcessed)
if InCamera == true and Players.LocalPlayer.Team == Teams.Controller then
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.W then
HoldingW = true
task.wait()
elseif input.KeyCode == Enum.KeyCode.A then
HoldingA = true
task.wait()
elseif input.KeyCode == Enum.KeyCode.S then
HoldingS = true
task.wait()
elseif input.KeyCode == Enum.KeyCode.D then
HoldingD = true
task.wait()
end
-- had an idea to implement a ton of if statements here to check which keys are currently being held, and relay the end result to the server via a remoteevent, didnt implement because itd be highly inefficient,
end
end
end)
UIS.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.W then
HoldingW = false
task.wait()
elseif input.KeyCode == Enum.KeyCode.A then
HoldingA = false
task.wait()
elseif input.KeyCode == Enum.KeyCode.S then
HoldingS = false
task.wait()
elseif input.KeyCode == Enum.KeyCode.D then
HoldingD = false
task.wait()
end
end
end)
The user is in a seat during all of this by the way, its kind of like a security camera system.
Please remember that the server can see the position of the user’s character, so you could instead make it so the lasers move relative to the players current position. Through the application of something like tweening, you could make it so the lasers are trying to move to match the position of the player, and then variate the speed of such action to allow for accurate perpendicular movement. This is exactly how I create smooth physics based projectiles.
From a servers point of view, the only operations it should be making are ones which should be reflected for all clients - if you are wanting a laser grid out of something like Resident Evil where it matches the moves of the person inside, then this should be done on the client and the general change sent to the server itself.
What do you want the system to actually do in the end product?
Hey, sorry for not clarifying earlier, the User is going to be in a seat during all of this. So while they’re pressing WASD they wouldnt be moving at all. I’m looking to have a system where the User in a seat would be viewing the map from a fixed cframe, like a security camera (which I didnt have problems with), and be able to control the movement of two perpendicular lasers via WASD so it feels more natural.
Hey, so reading this scenario I completely agree with the method your using the capture the inputs.
In regards to actually applying such inputs to the lasers:
If precision is important, use RenderStepped or Stepped to make sure the laser maths is done before any simulations are done - this can slow the user experience if the operation is large though, so use with caution.
If precision is not important (which to be honest, in your situation this is likely the case as I can think of few occurrences where such precision is necessary - maybe when trying to land a probe on an asteroid?) then I would use Heartbeat to achieve the end goal. Simply make it do an operation every time the frame is called - this also wont slow the game at all.
Therefore, you would simply do:
game.RunService.Heartbeat:Connect(function()
-- If a player is holding a key, do this....
end)
Ah ok, thanks for the explanation. So the server script managing the movement of the lasers would get the data regarding what keys are held via a standard remote fired every time UIS detects a key being held in the local script right?
You should use the MoveDirection of the humanoid to detect if they’re moving instead of detecting if they’re using “WASD”. Then from there you wouldn’t need a loop since It already detects if you’re moving. Same goes for running event, I mean I see a benefit for using MoveDirection/Running event over using a loop.
In an even more simple way, if you need 1 user to represent the change in movement of the lasers, you could make the server echo the change to the other clients.
What I would personally do from a security standpoint is a virtualised laser system:
Make the client fire to the server a list of inputs when an input has changed (this prevents the user spamming the server).
The server has a store of the current inputs being applied to the laser - these change when the above event is fired. Make sure you check that the person seated currently, is the same person giving in the input.
The server, using the heartbeat service makes the informed decision based on the current inputs how to affect the lasers. The server then fires the clients the current location of both lasers. The server should not set the position of the visual lasers itself as it can make the game look tacky.
On the client side, the client receives the current position of the laser and does a tween to such location (or if you use the below example, have the visual laser always tween to the hitbox) - this therefore makes the operation seem smooth.
In order to allow for hit detection, if you have an invisible laser hit box, you can make the server move this hitbox as its invisible which keeps the game looking smooth whilst having a visual which is the same for all clients.
If you do want to trust the client, then go right ahead and just use a virtualised system though - if you trust the client to do their own hit detection you can just make the clients reflect where the server believes it to be at each instance.
Just please don’t do a constant while loop, it will make the game throttle.
That sounds like the best implementation of the system to me. Just to make sure I understand though, essentially what you’re saying is:
the client in control of the lasers and server will both keep tabs on what WASD keys are currently being pressed by storing them in an array
The client in control uses said array to determine if firing a remote event is appropriate so the server isnt flooded with repetitive events
The server uses said array to determine how exactly the pair of lasers should be tweened and to what position
The client in control tweens the position of the lasers on their end in order to not make the movement feel unresponsive
The server fires an event telling all other clients to do the same, while actually moving an invisible hitbox on the serverside so again it doesn’t feel unresponsive
If I remember correctly this method of reducing visible latency is also used for gun systems, so I can see how itd make sense here. Thanks for the help.
Yeah, so on the serverside the Lasers aren’t moving at all? The server will just repeatedly call remotes to order all clients to tween the Lasers to a position 0.1 studs in whatever direction on their end right?
And yeah if this is the case detection has to be done on the client side so that where the laser is for the client is where they’d die if they touched it, not somewhere else slightly behind.