Collisions in a grab script

so im making a script which worked out fine but one thing that annoys me is the collisions

any part that i grab gets clipped halfway into other objects due to the script only keeping count on the collisions of the mouse. How can i fix that?

Local script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local grabbedObject = nil
local bodyPosition = nil
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local moveObjectEvent = ReplicatedStorage:WaitForChild("MoveObjectEvent")

-- Function to handle grabbing or releasing the object
local function onObjectClicked(object)
	if grabbedObject == object then
		-- Release the object
		moveObjectEvent:FireServer(grabbedObject, grabbedObject.Position, true)
		grabbedObject.CanCollide = true
		grabbedObject.Anchored = false
		if bodyPosition then
			bodyPosition:Destroy()
		end
		mouse.TargetFilter = nil
		grabbedObject = nil
		bodyPosition = nil
	else
		-- Grab the object
		grabbedObject = object
		grabbedObject.CanCollide = false
		grabbedObject.Anchored = false
		bodyPosition = Instance.new("BodyPosition")
		bodyPosition.MaxForce = Vector3.new(4000, 4000, 4000)
		bodyPosition.P = 10000
		bodyPosition.D = 1000
		bodyPosition.Parent = grabbedObject
		mouse.TargetFilter = grabbedObject
	end
end

-- Connect ClickDetector events
local function setupClickDetector(object)
	local clickDetector = object:FindFirstChildOfClass("ClickDetector")
	if clickDetector then
		clickDetector.MouseClick:Connect(function()
			onObjectClicked(object)
		end)
	end
end

-- Connect ClickDetector events for existing objects
for _, object in ipairs(workspace:GetChildren()) do
	setupClickDetector(object)
end

-- Listen for new objects added to workspace
workspace.ChildAdded:Connect(function(child)
	setupClickDetector(child)
end)

-- Update the object's position
game:GetService("RunService").RenderStepped:Connect(function()
	if grabbedObject and bodyPosition then
		local mousePosition = mouse.Hit.Position
		bodyPosition.Position = mousePosition
		moveObjectEvent:FireServer(grabbedObject, mousePosition, false)
	end
end)

Global script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local moveObjectEvent = ReplicatedStorage:WaitForChild("MoveObjectEvent")

-- Function to handle moving the object on the server
local function onMoveObject(player, object, newPosition, release)
	if object and object:IsA("BasePart") then

		object.Position = newPosition
		object.Anchored = not release
		object.CanCollide = release

		-- If release is true, indicating the object should be released
		if release then
			-- Set Anchored to false to release the object
			object.Anchored = false
			-- Set CanCollide to true to enable collisions
			object.CanCollide = true
			-- Perform any additional cleanup or handling here
			-- For example, you can remove the object from the player's inventory or reset any holding state
		end
	end
end

-- Connect RemoteEvent
moveObjectEvent.OnServerEvent:Connect(onMoveObject)

I have exactly the same problem but no one is responding, I’ll let you know if I find it

2 Likes
local PhysicsService = game:GetService("PhysicsService")
PhysicsService:RegisterCollisionGroup("DefaultPlayer")
PhysicsService:RegisterCollisionGroup("Carrying")

PhysicsService:CollisionGroupSetCollidable("DefaultPlayer", "Carrying", false)


										for Index, TargetCollision in pairs(TargetChar:GetDescendants()) do
											if TargetCollision:IsA("BasePart") or TargetCollision:IsA("MeshPart") or TargetCollision:IsA("UnionOperation") then
												PhysicsService:SetPartCollisionGroup(TargetCollision, "Carrying")
												TargetCollision.Massless = true
											end
										end

this is how you can change the collision

When a player appears, give him a DefaultPlayer collision, when the player is lifted, give him a Carrying collision.

BodyPosition is not registered in the global script

I’m giving you a script, but if you want to keep yours, I think a change would be the solution. In your global script, on line 8, you change the position of your object, but you would need to change the BodyPosition.Position
(Place this local script in StarterCharacterScripts)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local holding = false
local object

mouse.Button1Down:Connect(function()
	if holding == false then
	if mouse.Target and mouse.Target:IsA("BasePart") and mouse.Target:FindFirstChildOfClass("ClickDetector") then
		object = mouse.Target
		holding = true
		object.Anchored = false
		object.CanCollide = true

		-- Créer un BodyPosition pour déplacer l'objet
		local bodyPosition = Instance.new("BodyPosition")
		bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- Augmentation de la force maximale
		bodyPosition.D = 2000 -- Augmentation de l'amortissement
		bodyPosition.P = 60000 -- Augmentation de la puissance
		bodyPosition.Parent = object

		-- Créer un BodyGyro pour stabiliser l'objet avec son orientation initiale
		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge) -- Force maximale pour contrer la rotation
		bodyGyro.D = 2000 -- Amortissement pour stabiliser rapidement
		bodyGyro.P = 60000 -- Puissance pour maintenir l'orientation
		bodyGyro.CFrame = object.CFrame
			bodyGyro.Parent = object
			end
	else
			
			if object then
				mouse.TargetFilter = nil
				holding = false
				if object:FindFirstChildOfClass("BodyPosition") then
					object:FindFirstChildOfClass("BodyPosition"):Destroy()
				end
				if object:FindFirstChildOfClass("BodyGyro") then
					object:FindFirstChildOfClass("BodyGyro"):Destroy()
				end
				object = nil
			end
	end
	end)
	
game:GetService("RunService").RenderStepped:Connect(function()
	if holding and object and object:FindFirstChildOfClass("BodyPosition") and object:FindFirstChildOfClass("BodyGyro") then
		
		-- Calculer la nouvelle position de l'objet
		mouse.TargetFilter = object
		local newPosition = mouse.Hit.Position

		object:FindFirstChildOfClass("BodyPosition").Position = newPosition
		object:FindFirstChildOfClass("BodyGyro").CFrame = object.CFrame -- Maintenir l'orientation initiale de l'objet
	end
end)

I send a message again so that you receive a notification and read my changes