Trouble with grabbing (and throwing) objects with the player's mouse

Salutations, all. As the title suggests, I am having issues with picking up and throwing objects. I understand that the main problem may be because I’m handling it on the client, but I thought :SetNetworkOwner() let this happen, or perhaps I’m misinterpreting it. Regardless, they appear to sink on the server side yet are fine on the client.

My client code:

game:GetService("RunService").RenderStepped:Connect(function()
	if GrabbedObject then
		GrabbedObject:PivotTo(WorldMouse.CFrame * CFrame.new(5, 0, 0))

	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(Input, Processed)
	if not Processed then
		if Input.UserInputType == Enum.UserInputType.MouseButton1 then			
			if Mouse.Target:IsDescendantOf(workspace:WaitForChild("Workspace"):WaitForChild("Pickupables")) and Mouse.Target.Parent:IsA("Model") and not Mouse.Target.Parent:GetAttribute("Grabbed") then
				GrabbedObject = Mouse.Target.Parent
				
				script:WaitForChild("RemoteEvent"):FireServer("MoveObject", GrabbedObject)

			end
		end
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(Input, Processed)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 and GrabbedObject then
		script:WaitForChild("RemoteEvent"):FireServer("DropObject", GrabbedObject)
		
		GrabbedObject = nil
		
	end
end)

My server code:

script.Parent:WaitForChild("RemoteEvent").OnServerEvent:Connect(function(Player, Type, Arg1)
	if Type and Type == "MoveObject" then
		GrabbedObject = Arg1

		for i,v in pairs(GrabbedObject:GetDescendants()) do
			if v:IsA("BasePart") then
				v:SetNetworkOwner(Player)

			end
		end
				
		GrabbedObject:SetAttribute("Grabbed", true)
		
	elseif Type and Type == "DropObject" then
		GrabbedObject = Arg1
		GrabbedObject:SetAttribute("Grabbed", false)
				
		for i,v in pairs(GrabbedObject:GetDescendants()) do
			if v:IsA("BasePart") then
				v:SetNetworkOwner(nil)

			end
		end
	end
end)

Client view:

Server view:

If anyone can help, then cheers!

1 Like

You can’t just do this immediately after the client lets go. You need to wait a few seconds.

1 Like

Well, I set to a 0.1 delay and it might(?) have improved the throwing, however my main issue still lies in that the picked up objects are sinking into the ground on the server side. I do know the reason why, however I’m unsure on how to solve it, is my problem.

Actually, do pardon my previous comment. After a bit more playing around, it seems to be better off than where it was previously, though still not perfect. C’est la vie, though.

Here’s what I changed:

game:GetService("UserInputService").InputEnded:Connect(function(Input, Processed)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 and GrabbedObject then
		script:WaitForChild("RemoteEvent"):FireServer("DropObject", GrabbedObject)
		
		task.wait(0.1)
		
		GrabbedObject = nil

	end
end)
	elseif Type and Type == "DropObject" then
		GrabbedObject = Arg1
		
		task.wait(1)
				
		for i,v in pairs(GrabbedObject:GetDescendants()) do
			if v:IsA("BasePart") then
				v:SetNetworkOwner(nil)

			end
		end
		
		GrabbedObject:SetAttribute("Grabbed", false)

	end

Begrudgingly, I also did add a server mouse tracking, which did solve my sinking issue.

game:GetService("RunService").RenderStepped:Connect(function()
	if GrabbedObject then
		GrabbedObject:PivotTo(WorldMouse.CFrame * CFrame.new(5, 0, 0))
		
		script:WaitForChild("RemoteEvent"):FireServer("MoveObjectToMouse", GrabbedObject)

	end
end)
	elseif Type and Type == "MoveObjectToMouse" then
		GrabbedObject:PivotTo(WorldMouse.CFrame * CFrame.new(5, 0, 0))

Though I’m quite unsure why it’s now doing this:

Something to do with network ownership. What if it was set to the client still?

1 Like

That might have been it? I moved the MoveObjectToMouse server fire to another part of the script and it stopped freaking out when being dropped. Still having trouble throwing, but I’ll figure something out. Thank you!

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