Player holding box on the server

I’ve created a script that lets a player “hold” a box (PrimaryPart) in front of them.

holdingBox.Changed:Connect(function()
	if holdingBox.Value then
		local box = holdingBox.Value
		while holdingBox.Value do
			local lookCF = CFrame.new(Player.Character.Head.Position, Mouse.Hit.Position)
			local CF = lookCF * CFrame.new(0, 0, -7) 
			if CF then
				Tween = TweenService:Create(box, TweenInfo.new(.1), {CFrame = CF})
				Tween:Play()
				--Events.UpdateBoxPosition:FireServer(box, CF) 
			end
			wait()
		end
	elseif Tween then
		Tween:Cancel()
	end
end)

Not shown above, the parts are initially not anchored, but then are anchored when a player picks up the box.

Obviously, because I’m using the Player’s mouse position, this is client sided, and so the movement of the box isn’t shown on the server. (See: https://gyazo.com/bbdf3601aaa3a15b2cfc8662da295715)

I’ve tried sending a remote so the server will handle the tweening of the box (seen in the commented line), but I don’t like how rigid the movement of the box is as seen by the player holding it (See: https://gyazo.com/e160960ddbfa2e8102375e6d414dfabb)

Is there a better way to move the box on the server? Ideally, I’d like the box to smoothly follow where the player is looking (mouse position), but I’m not sure its a good idea to send remotes to the server multiple times per second through potentially multiple different clients.

Okay so I “fixed” the problem by just tweening the block on both the client and the server, so the movement is detected on the server and the client still sees a fluid motion.

I know this is a very hacky solution, but it’s the best I’ve got so far. I’m going to tamper with how often I update the server with the box’s position so I don’t destroy performance.

Hello,

I do not suggest doing any sort of movement on the server as the server can sometimes lag and is less smooth. Just like when you make pets, you shouldn’t handle the movement of the pets on the server as they wouldn’t be as smooth.

The server should only be used to parent the part to the workspace (if it isn’t already the case)
Then what you need to do is to find a way to handle the movement on the client (on all clients). When making pets, you move every player’s pet under a RenderStepped event in all clients so that everything runs smoothly and you take away a lot of stress off the server. I am not entirely sure how you would implement this to your situation but that’s my take.

I didn’t know pets worked mostly on the client. How does that work exactly? Does each client update the pets based on the positions of their respective owners? As you said, that would obviously be different in my case because I’m including the mouse position in the CFrame.

I don’t need the server position to be perfect, but I need to it be close. What I’ve opted to do now is the client will make a clone of the box, and turn the real box invisible. That clone will update every loop on the client, and the real box will be updated on the server every 5 passes of the loop.
(See: https://gyazo.com/3d76ebf67a47ff7c91717ec735c3d20f - The real box is red in this gif to show what I mean.)

I’m still worried about firing too many remotes to the server, but this somewhat cuts it down. Do you think this method of moving two different objects on the server and client would work, or might it be better to do everything on the client?

What concerns me is how you would get the mouse position of the client which is moving the part from other clients which makes it highly annoying to make it all on client which is why I can’t give you a proper solution.

For pets it’s easy cause you iterate through every player, check if the player has any pets and if they do you move them according to the Torso Position within a RenderStepped event.

What you may try to do is you pass the CFrame to the Server just like you are doing now but then you fire a remote event with :FireAllClients() with that CFrame as an argument and work with that but I am not sure of how good this would work.