I want to make a player-controlled ball which can move left and right. It will have a continuous forward velocity and all movement will be handled on the server. I also want to compensate for client-to-server latency and make sure that the system is exploit-proof.
Currently, I have a system that works, except the left/right movement is really slow and doesn’t feel smooth and responsive. Here’s a video of how the system is currently:
Current System Video
Here’s a link to the test place in case you want to see for yourself how annoyingly slow it is.
I want to make the system work so that when I press the A/D keys on my keyboard, the ball will move quicker and will accelerate much faster left/right. I also want to make sure that there is little to no lag.
Here is my server and client code. It’s very basic and uses BodyMovers, which are deprecated.
Client Code (StarterPlayerScripts)
local camera = game.Workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local marble = game.Workspace:WaitForChild("Marble")
local movementEvent = game.ReplicatedStorage:WaitForChild("Movement")
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
movementEvent:FireServer(marble, "Left", true)
elseif input.KeyCode == Enum.KeyCode.D then
movementEvent:FireServer(marble, "Right", true)
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
movementEvent:FireServer(marble, "Left", false)
elseif input.KeyCode == Enum.KeyCode.D then
movementEvent:FireServer(marble, "Right", false)
end
end)
Server Code (ServerScriptService)
local forceVelocity = 500
local directionalVelocity = 1000
local bv = Instance.new("BodyForce")
bv.Name = "ForwardForce"
bv.Force = Vector3.new(forceVelocity,0,0)
bv.Parent = workspace:WaitForChild("Marble")
local movement = game.ReplicatedStorage:WaitForChild("Movement")
movement.OnServerEvent:Connect(function(player, marble, movementType, callType)
if callType == true then
-- if movement began
if movementType == "Right" then
bv.Force += Vector3.new(0,0,directionalVelocity)
else
bv.Force += Vector3.new(0,0,-directionalVelocity)
end
else
-- movement ending
bv.Force = Vector3.new(forceVelocity,0,0)
end
end)
All help is greatly appreciated! Thanks in advance! If you need clarification, feel free to ask.