Replicating part movement caused by clients mouse to the server

I’m currently in the making of a feature where the client can drag objects around the map using their mouse but I’ve come across an issue. I want all movements to be visual to all players but I don’t know the correct way to do this. I’ve already tried to fire a remote event but firing as rapidly as I need will cause issues. I’ve also tried setting the Network ownership to the player but this does not work due to the objects being picked up are anchored. What would you say the best way I can achieve this is?

Local Script (Mouse movement):

local LocalPlayer = Players.LocalPlayer
local LocalMouse = LocalPlayer:GetMouse()

local MouseButton1Down = false
local MouseTarget

-- [[Functions]] --
local function inputBegan()
	MouseTarget = LocalMouse.Target
	
	if MouseTarget then
		LocalMouse.TargetFilter = MouseTarget
		if string.find(MouseTarget.Name, "Patty") then
			if MouseTarget.Anchored then
				if MouseTarget:GetAttribute("Draggable") then
					MouseButton1Down = true
				end
			end
		end
	end
end

local function inputEnded()
	if MouseTarget then
		if string.find(MouseTarget.Name, "Patty") then
			local target = MouseTarget
			
			MouseButton1Down = false
			MouseTarget = nil
			LocalMouse.TargetFilter = nil
			
			local RaycastParameters = RaycastParams.new()
			RaycastParameters.FilterDescendantsInstances = {game.Workspace.Stove1.CookingRegion}
			RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
			
			local RayResults = workspace:Raycast(target.Position, (Vector3.new(target.Position.X, target.Position.Y - 50, target.Position.Z) - target.Position), RaycastParameters)

			if math.round(RayResults.Distance) >= 1 then
				target.Position = Vector3.new(target.Position.X, target.Position.Y - (RayResults.Distance - (target.Size.Y / 2)), target.Position.Z)
			end
			
			NetworkEvent:FireServer(target, target.CFrame)
		end
	end
end

local function renderStepped()
	if MouseTarget and MouseButton1Down then
		NetworkEvent:FireServer(MouseTarget, CFrame.new(Vector3.new(LocalMouse.Hit.Position.X, LocalMouse.Hit.Position.Y + 1, LocalMouse.Hit.Position.Z)))
	end
end

-- [[Events]] --
LocalMouse.Button1Down:Connect(inputBegan)
LocalMouse.Button1Up:Connect(inputEnded)

RunService.RenderStepped:Connect(renderStepped)

It shouldn’t cause issues if you fire a RemoteEvent with the Mouse.Hit every frame and then set it on the server.

The only thing is there might be slight latency delay. So if you want you can set it on both the client and the server so that it is smooth for the client and everyone else can see it.

This does work but it isn’t very smooth and can be very julty at times as you mentioned even when I update the position on both the client and the server. Are there any other known ways or might this be my only option?

It should be perfectly smooth if you update it every frame.

Are you using RunService.RenderStepped?


Also if you don’t want to send a RemoteEvent every frame you can send it less often and use a tween to interpolate it on the server.

Okay wait, I tried to only update the position on the server side rather than both and I got a much smoother result. My only concern now is the amount of lag this might cause.

If you don’t want to send a RemoteEvent every frame you can send it less often and use linear interpolation to make it smooth. (Tween the CFrame position from the last one to the new one)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.