How to remove movement delay?

I am trying to make the controls for my game that move a cube according to a table I assign per change, as well as doing that without any movement delays after executing the specified keys.

The delay in movement is too frustrating for players to use, as it acts roughly 100ms after a key press due to client-server communication delays, which is too slow when it comes to platforming across the cubed map, which can be particularly difficult to navigate.

I have tried looking for NetworkOwner logic and seeing if I can utilize it here, but it only caused another error when I tried to implement it. I may be using it wrong or it might simply just not work. The Developer Hub didn’t seem to contain anything of what I need to make movement more responsive when it comes to my current game.

CODE

Server Script

local Players = game:GetService("Players")
local controls = {}
local jumpcd = {}

Players.PlayerAdded:Connect(function(plr)
	controls[plr.Name] = {}
	jumpcd[plr.Name] = 0
end)

local cevent = game:GetService("ReplicatedStorage").TestPlatformerRemotes:FindFirstChild("ControlEvent")
cevent.OnServerEvent:Connect(function(plr, contdata)
	controls[plr.Name] = contdata
	print("Pong!")
end)

game:GetService("RunService").Stepped:Connect(function()
	for _,p in ipairs(Players:GetChildren()) do
		local con = controls[p.Name]
		local pc = p.CStats.Character.Value
		
		if con.left and pc.AssemblyLinearVelocity.Y > -10 then
			pc.AssemblyLinearVelocity = pc.AssemblyLinearVelocity + Vector3.new(-4,0,0)
		elseif con.right and pc.AssemblyLinearVelocity.Y < 10 then
			pc.AssemblyLinearVelocity = pc.AssemblyLinearVelocity + Vector3.new(4,0,0)
		end
		
		
		if pc.AssemblyLinearVelocity.Y < .6 and pc.AssemblyLinearVelocity.Y > -.6 then
			if jumpcd[pc.Name] > 0 then
				jumpcd[pc.Name] = jumpcd[pc.Name] - 1
			end
		end
		
		if con.jump and jumpcd[pc.Name] <= 0 then
			pc.AssemblyLinearVelocity = Vector3.new(pc.AssemblyLinearVelocity.X, math.random(50, 75), pc.AssemblyLinearVelocity.Z)
			jumpcd[pc.Name] = 6
		end
	end
end)

Local Script

local UIS = game:GetService("UserInputService")
local cont = {
	left = false,
	right = false,
	jump = false
}

local cevent = game:GetService("ReplicatedStorage").TestPlatformerRemotes:FindFirstChild("ControlEvent")
local pc = workspace["!Players"]:FindFirstChild(game:GetService("Players").LocalPlayer.Name)

game:GetService("RunService").Heartbeat:Connect(function()
	local dataChanged = false
	local locont = {
		left = UIS:IsKeyDown(Enum.KeyCode.A),
		right = UIS:IsKeyDown(Enum.KeyCode.D),
		jump = UIS:IsKeyDown(Enum.KeyCode.Space)
	}
	
	if cont.left ~= locont.left then
		dataChanged = true
		cont.left = locont.left
	elseif cont.right ~= locont.right then
		dataChanged = true
		cont.right = locont.right
	end
	
	if cont.jump ~= locont.jump then
		dataChanged = true
		cont.jump = locont.jump
	end
	
	if dataChanged then
		cevent:FireServer(cont)
		print("Ping!")
	end
end)

Most notably, all scripts unrelated to this is working correctly. There are no errors or warnings, only an annoying movement delay that is found in live games exclusively due to the client and server no longer being emulated in the same machine.

The Player Character is a Part, and that Part exists in a workspace folder (and was temporarily put in the workspace to see if changing network owners would fix even bothering with client-server communications).

I am not sharing much more of my scripts, as I want to keep the project unique and impossible to copy effectively.

Any help and alternate methods are appreciated!

This is a bit beyond my reach, but i was thinking since i came across this post before:

have you tried to use RenderStepped instead of Stepped?

Are you even using Network owner in any of the scripts? If not, then you definitely should. To set a Network owner it has to be done via server script. In a server script, try setting the network owner to the client that way the client is smooth but the server is the one with the slight delay (which is completely normal)

The primary cause of the input lag is because of a RemoteEvent I depend on. The RenderStepped function works as well as Stepped, and I know that because I tested on the Studio (no client-server lag) and the server acted immediately.

Already tried that, already failed. Doing it on the server just breaks the camera and positioning, and seemingly the entire game in the process. Do I have to use a local script to position the cube via the player’s client?

i dont really see the point of updating the movement in the server
just update it in the client so it wont delay

if your Movement script is a server script, then that is most likely why it has a slight delay.
Changing it to the client would probably fix it and you could utilize remote events to send the movement you made to the server that way other players could see it.

1 Like

I placed a critical segment of the code for movement into the local script and then tested it on a live game. The cube acted with zero delay right off of the bat, and the server follows any moves that are made. Thanks for the help!

1 Like