Roblox weld collision is wonkey

Hello! I have found this bug where my box that is welded to my hrp is uhh having some collision problems, when i press any movement key on the wall, two or three times, it just clips through the wall

https://gyazo.com/6749649f91e2a32005a8605c176bfc3f

the box was’t supossed to go through the wall

script:

local player = game.Players.LocalPlayer
local hrp = script.Parent.HumanoidRootPart
local humanoid = script.Parent.Humanoid
local Holding_WalkAnim = humanoid:LoadAnimation(workspace.walk)
local UIS = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local mouse = player:GetMouse()
local torso = script.Parent.Torso
local isBoxWeldSpawned = false

rs.Heartbeat:Connect(function()
    wait()
    if mouse.Target and mouse.Target:FindFirstChild("Pickable") then
        local CD = mouse.Target.ClickDetector
        local Box = mouse.Target
        CD.MouseClick:Connect(function()
            if isBoxWeldSpawned == false then
                isBoxWeldSpawned = true
                weldRightArm = Instance.new("WeldConstraint")
                weldRightArm.Name = "RightArmWeld"
                Box.CFrame = hrp.CFrame * CFrame.new(0,0,-1.70)
                weldRightArm.Part0 = torso
                weldRightArm.Part1 = Box
                weldRightArm.Parent    = script.Parent
                Holding_WalkAnim:Play()
            end
        end)
    end
end)

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        weldRightArm.Parent = nil
        Holding_WalkAnim:Stop()
        isBoxWeldSpawned = false
    end    
end)

I’ve seen some weird behavior if you dont set the parent of the weld to the workspace or part before you set the part1 and part0s, see if that works?

Edit: This is just speculation on my part so dont take my word for it.

A couple notes…

  • You have a major memory leak in your code. When you mouse over an object, it connects to ClickDetector.MouseClick 60 times per second. This means that your innermost function is running many, many times. Additionally, it’s not disconnecting, so those connections are gunna stick around. You should focus on fixing this issue first.
  • It’s better if you use ContextActionService BindAction and UnbindAction instead of UserInputService.InputBegan here, as your code will run even when the player is typing in chat or doing other things with their E key.
  • Don’t use RunService.Heartbeat for this. If anything, use Stepped instead. The wait() in your code screams “my code has a race condition”, and that’s a big indicator of other issues.
  • You shouldn’t use a ClickDetector for this. Instead, when you first mouse over a “Pickable”, you should use BindAction to start listening for MouseClicks (or gamepad inputs, if you swing that way). If you’re no longer hovering over the same pickable, then call UnbindAction.
2 Likes

This isn’t the issue – it’s generally better to set Part0/Part1 before Parent anyway. If it’s really a physics issue, then RootPriority needs to be set, and perhaps Massless.

1 Like

Ok! Thank you i will try that, i am a very new scripter and have begun like 3 months ago, thanks for the help!

But my question is why is it not good to use click detector?

also another question of mine is, is this script better does it have anymore problems?

script local function SpawnBox()
	if isGroundPounding == false then
		isBoxWeldSpawned = true
		BoxIsWelded = true
		weldRightArm = Instance.new("WeldConstraint")
		weldRightArm.Name = "RightArmWeld"
		Box.CFrame = hrp.CFrame * CFrame.new(0,3.4,0)
		weldRightArm.Part1 = hrp
		weldRightArm.Part0 = Box
		weldRightArm.Parent	= workspace
		Holding_WalkAnim:Play()
	end
end

local function PickupBox()
		local pickable = mouse.Target:FindFirstChild("Pickable")
		if mouse.Target and pickable then
			Box = mouse.Target
			if isBoxWeldSpawned == false then
				if pickable.Value == true then 
					CAS:UnbindAction("groundpound")
					SpawnBox()
				end
			end
		end
	end

local function BoxThrow()
	if BoxIsWelded == true then
		if isSlideKicking == false then
			if isGroundPounding == false then
				weldRightArm.Parent = nil
				Holding_WalkAnim:Stop()
				BoxIsWelded = false
				isBoxWeldSpawned = false
				wait()
				Box.Velocity = Box.CFrame.LookVector * 30
				Holding_ThrowAnim:Play()
				wait()
				Holding_ThrowAnim:Stop()
			end	
		end
	end
end

CAS:BindAction("ThrowBox", BoxThrow, false, Enum.KeyCode.E)
CAS:BindAction("PickupBox", PickupBox, false, Enum.UserInputType.MouseButton1)

Sorry for the delay in response. To answer your questions:

It’s not necessarily the ClickDetector itself, but rather how you’re using it. Ideally, every time a new object that is “Pickable” (with a ClickDetector in it) has its MouseClick event connected to your pickup function. Then, you wouldn’t ever have to deal with mouse.Target.

That said, ClickDetector is a very old class. Although it still works, something like this is better done using ContextActionService like you’re doing now.

As for your second question…

When you bind a function using ContextActionService:BindAction, CAS calls your function for every input state when the relevant input is done. So, when for key presses, it calls twice: once for key down and once for key up. Your handler should look something like this:

local function BoxThrow(actionName, inputState, inputObject)
    -- Only consider "key up" inputs:
    if inputState ~= Enum.UserInputState.End then return end
    -- ...
end

That way, you ignore the first “key down” input state, and actually listen to only “key up” states.

Besides that, just one tip when it comes to testing true/false values in an if statement:

You don’t need to use == true with bools. They’re either already true, or false. So you can do if bool then instead of if bool == true then. Similarly, when testing for false, you can use the not keyword: if not bool then instead of if bool == false then. It’s ever so slightly more performant, and it reads nicer :slight_smile:

Godspeed to your coding journey!

1 Like

Thanks, alot! I appreciate your help very much, and hope i will succed, and that you will succed in your coding journey’s as well! GodSpeed my friend!