Issue replicating a player drag system

I have a simple drag system where players can drag models with anchored parts around. It works well but the issue I have is with replication.

When a player is dragging something I only replicate it a few times a second as lots of players will be dragging models at once.

If a client is dragging a part around quickly it slightly rubber bands back to where it was a moment ago as the server position was sent back to the client.

So my question is can I somehow stop the player moving a model from getting position updates from the server.

Thanks :slight_smile:

EDIT:
Here is the code:

Client:

local UserInputService = game:GetService("UserInputService")
local workspace = game:GetService("Workspace")
local camera = workspace.CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()

local handleInteractionEvent = game:GetService("ReplicatedStorage"):WaitForChild("clientInteraction")

function GetHighestParent(part)
	if part.Parent == workspace then
		return part
	end
	return GetHighestParent(part.Parent)
end

function clampPlayerReach(playerVector,dragVector,num)
	local x = math.clamp(dragVector.X, playerVector.X-num,playerVector.X+num)
	local y = math.clamp(dragVector.Y, playerVector.Y-num,playerVector.Y+num)
	local z = math.clamp(dragVector.Z, playerVector.Z-num,playerVector.Z+num)
	return Vector3.new(x,y,z)
end


local lastReplication = tick();
local function handelDragReplication(model,loc)
	local delta = tick()-lastReplication
	if(delta >= 0.25) then
		handleInteractionEvent:FireServer("posrep",model, loc)
		lastReplication = tick()
	end
end


UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local target = GetHighestParent(mouse.Target)
		local canGrab = target:FindFirstChild('canGrab')
		if(canGrab and canGrab.Value) then
		mouse.TargetFilter = target
		repeat
			if(mouse.Target) then --If the mouse isn't targeting a part igonre
				local loc = clampPlayerReach(Character.PrimaryPart.Position,mouse.Hit.Position, 50)
				target:MoveTo(loc)
				handelDragReplication(target, loc)
			end
			wait()
		until input.UserInputState == Enum.UserInputState.End
		mouse.TargetFilter = nil;
		end
	end
end
)

Server side:

local handleInteractionEvent = game:GetService("ReplicatedStorage"):WaitForChild("clientInteraction")
local TweenService = game:GetService("TweenService")

local function handelPosRep(model, data)
	model:MoveTo(data)
end

local function handleCall(player, command, model, data)
	if(command == "flip") then
		--Ignore
	elseif(command == "posrep") then
		handelPosRep(model, data)
	end
end

-- Connect function to event
handleInteractionEvent.OnServerEvent:Connect(handleCall)

I also uploaded a place with this code: here
It seems a bit less of an issue in the place compared to in studio but its still there. My worry is that latency plays a part in how bad it is.

2 Likes

Are you able to provide a code snippet, or anything like that. I need to be able to see what you’re looking at.

I’ve added my code to the post. Thanks :slightly_smiling_face:

1 Like

Have the server set the network ownership of whatever is being dragged to the client. Doing this will give the client control over the thing and replicate it to the server without the server interfering.

After doing this you shouldn’t need to handle position replication with a script either, just need to set the network ownership back to automatic/server after the client finishes dragging the object.

Sorry I forgot to mention but my parts are anchored. Correct me if I’m wrong but I don’t think you can set ownership of anchored parts.

You’re right, unfortunately that complicates this a lot more. I’d just recommend unanchoring it on the server, and anchoring it on the client as a work-around. If you don’t want to do that though, I’d suggest finding what step on the task scheduler (stepped, heartbeat, etc) the position of the model is replicated, and then changing the position to what it was before the replication to override the replication.

Unanchoring on the server did the trick. Thank you for the help! :grinning:

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