Object going through wall when holding

I made a system that allows me to pickup and hold objects.

when I am holding the object, it clips through walls essentially making it useless for the type of game I am making.

Every solution I have tried finding does not work.

local function HoldObject()
	if not HoldingObject and CheckForHoldable() then
		
		local Object = GetHoldable()
		HoldingObject = true
		CurrentHolding = Object
		
	end
end

local function ReleaseObject()
	if HoldingObject and CurrentHolding ~= nil then
		
		CurrentHolding.CanCollide = true
		CurrentHolding.Anchored = false
		
		CurrentHolding = nil
		HoldingObject = false
	end
end


-- this function below is ran in RenderStepped
local function Update()
	if HoldingObject and CurrentHolding ~= nil then
		
		CurrentHolding.CFrame = CurrentHolding.CFrame:Lerp(Camera.CFrame * CFrame.new(0, 0, -4.5), 0.1)
		CurrentHolding.CanCollide = false
		CurrentHolding.Anchored = true
		
	end
end

Help please!

In the function

local function HoldObject()
	if not HoldingObject and CheckForHoldable() then
		
		local Object = GetHoldable()
		HoldingObject = true
		CurrentHolding = Object
		
	end
end

Maybe say

CurrentHolding.CanCollide = true

after you define currentholding.

This is the finished function if I’m sure. It’s basically just a line of code added onto it

I set it to true in both the holdobject() and update() and it now only collides with the player and still collides through walls.

Oh! I hadn’t seen the update function.

You’re running the update function with rendersteeped, where you’re also setting cancollide to false

local function Update()
	if HoldingObject and CurrentHolding ~= nil then
		
		CurrentHolding.CFrame = CurrentHolding.CFrame:Lerp(Camera.CFrame * CFrame.new(0, 0, -4.5), 0.1)
		CurrentHolding.CanCollide = false -- See this? It's false! It should be true!
		CurrentHolding.Anchored = true
		
	end
end

Cause you’re setting it to false every frame, it goes through walls. Set it to true and it should work

Yes I saw that and I fixed it but I am still having the issue.

Can you send the whole script cause there are things that are missing that could be the issue

--//
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")

--//
local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local PlayerGui = Player:WaitForChild("PlayerGui")
local Camera = workspace.CurrentCamera
local Mouse = Player:GetMouse()

--//
local AssetsFolder = ReplicatedStorage:WaitForChild("assets")
local RemotesFolder = AssetsFolder:WaitForChild("remotes")
local ModulesFolder = AssetsFolder:WaitForChild("modules")

--//
local CheckLength = 8
local LastHoldable = nil
local CurrentHolding = nil
local HoldingObject = false

--//
local function CheckForHoldable()
	local RayCast = Ray.new(Camera.CFrame.Position, Camera.CFrame.LookVector * CheckLength)
	local Wall, HitPos = workspace:FindPartOnRay(RayCast, Character)
	
	if Wall and CollectionService:HasTag(Wall, "Holdable") then
		return true
	else
		return false
	end
end

local function GetHoldable()
	local RayCast = Ray.new(Camera.CFrame.Position, Camera.CFrame.LookVector * CheckLength)
	local Wall, HitPos = workspace:FindPartOnRay(RayCast, Character)

	if Wall and CollectionService:HasTag(Wall, "Holdable") then
		return Wall
	else
		return false
	end
end

--//
local function HoldObject()
	if not HoldingObject and CheckForHoldable() then
		
		local Object = GetHoldable()
		HoldingObject = true
		CurrentHolding = Object
		CurrentHolding.CanCollide = true
		
	end
end

local function ReleaseObject()
	if HoldingObject and CurrentHolding ~= nil then
		
		CurrentHolding.CanCollide = true
		CurrentHolding.Anchored = false
		
		CurrentHolding = nil
		HoldingObject = false
	end
end

local function Update()
	if HoldingObject and CurrentHolding ~= nil then
		
		CurrentHolding.CFrame = CurrentHolding.CFrame:Lerp(Camera.CFrame * CFrame.new(0, 0, -4.5), 0.1)
		CurrentHolding.CanCollide = true
		CurrentHolding.Anchored = true
		
	end
end

--//
UserInputService.InputBegan:Connect(function(Key, Chat)
	if Chat then return end
	
	if Key.KeyCode == Enum.KeyCode.E then
		HoldObject()
	end
end)

UserInputService.InputEnded:Connect(function(Key, Chat)
	if Chat then return end
	
	if Key.KeyCode == Enum.KeyCode.E then
		ReleaseObject()
	end
end)

RunService.RenderStepped:Connect(Update)

Sounds stupid but is the wall’s cancollide set to true?

yes it is set to can collide, else it would be falling through the ground

I’m pretty sure this happens because you’re setting object CFrame to position your object, and that means your object changes position no matter what, physics don’t affect it in that case.

I think I know what’s going on here

Your block is definitely colliding with the walls.
The block is trying to push away from the wall, but you have the block locked into a position relative to the player. This would explain why the block moves out of the wall when you release it.

It would also explain why players (and I assume other rigid bodies) are still colliding with the box. Rigid bodies are still able to push themselves away from the block while the walls cannot.

1 Like

is there no solution to solving this ?

I see what you mean by this, I suppose I could try a Linear Velocity component?

You kinda need to redo the grab system and do it different way. I’m not that experienced with making drag stuff myself so I recommend to find a dev forum post about it. The first one that catched my sight was this.

I might be wrong though, maybe it’s not the case. You should try either way.

That post includes a link to this reference page about AlignPosition

This attempts to move the part and still allow it to interact with anchored objects. This could be the answer for the part clipping