Custom Movement System Problems

Above is a video of my rock rolling down a mountain, the movement system works by detecting Keydown and Keyup events on client and then will fireserver and make the movements happen, it work’s fine except for the input lag, and I’m assuming lag will only get worse with more players in the game. If anyone has any ideas on how to improve this system please let me know. I will include an example of my code below:

function OnKeyDown(InputType, GameProcessed)
	if GameProcessed == false then
		if InputType.KeyCode == Enum.KeyCode.Space then
			if DroppedBool.Value == false then
				Drop()
			else
				Jump()
			end
		elseif InputType.KeyCode == Enum.KeyCode.A then
			Left = true
			Right = false
			Movement()
		elseif InputType.KeyCode == Enum.KeyCode.D then
			Right = true
			Left = false
			Movement()
		elseif InputType.KeyCode == Enum.KeyCode.Left then
			Left = true
			Right = false
			Movement()
		elseif InputType.KeyCode == Enum.KeyCode.Right then
			Right = true
			Left = false
			Movement()
		end
	end
end
	

UserInputService.InputBegan:Connect(OnKeyDown)

function OnKeyUp(InputType, GameProcessed)
	if GameProcessed == false then
		if InputType.KeyCode == Enum.KeyCode.A then
			Left = false
		elseif InputType.KeyCode == Enum.KeyCode.D then
			Right = false
		elseif InputType.KeyCode == Enum.KeyCode.Left then
			Left = false
		elseif InputType.KeyCode == Enum.KeyCode.Right then
			Right = false
		end
	end
end
		
UserInputService.InputEnded:Connect(OnKeyUp)

function Movement()

while true do

if Right == true then

if Current ~= "Right" then

Current = "Right"

MovementRemote:FireServer("Right")

end

elseif Left == true then

if Current ~= "Left" then

Current = "Left"

MovementRemote:FireServer("Left")

end

else

MovementRemote:FireServer("Stop")

break

end

wait()

end

end

Sorry but for some reason it glitches when trying to put the above code in a box

You can use Part:SetNetworkOwner(Player) on the ball, it must be done on the server.
That way, you can handle the logic through the client and have immediate feedback, the replication will be done automatically on the server.

6 Likes

Alright thanks!

Make sure to set my post as answer of this topic so it is solved, this is for people looking for help for a similar problem, it will help them. ^^

Isn’t it possible to just change the Part’s Velocity from the client instead of using RemoteEvents for movement? I’m not sure how you’re doing the movement for the ball, but I find that’d be the best option to reduce latency.

I tried to do it locally and the ball just wouldn’t move, anyway the :SetNetworkOwner() completely fixed the issue, it’s super smooth now.