.Touched event not always firing?

I have a script that detects when a player hits a part named “Wall,” then sticks them to that wall for a walljump. Usually this works, however randomly, it doesn’t.
https://gyazo.com/c19aed36cd44049c5c95d683dc57f0f0

The code I’m using to check if the .Touched event is firing is this. “wantsToWalljump” changes only when the player presses down and releases the space bar. Using this I narrowed the issue down to .Touched not firing

Yes I know I could just use only the HumanoidRootPart, it’s just for testing.

character.HumanoidRootPart.Touched:Connect(function(touchedPart)
	if touchedPart.Name == "Wall" and wantsToWalljump == true then
		print("touched by "..character.HumanoidRootPart.Name)
	end
end)

character.Head.Touched:Connect(function(touchedPart)
	if touchedPart.Name == "Wall" and wantsToWalljump == true then
		print("touched by " ..character.Head.Name)
	end
end)

character['Right Arm'].Touched:Connect(function(touchedPart)
	if touchedPart.Name == "Wall" and wantsToWalljump == true then
		print("touched by "..character['Right Arm'].Name)
	end
end)

character['Left Arm'].Touched:Connect(function(touchedPart)
	if touchedPart.Name == "Wall" and wantsToWalljump == true then
		print("touched by "..character['Left Arm'].Name)
	end
end)

Is this a problem with my code, or is it a problem to do with Roblox itself? Is there a better method that’s simply lost on me?

1 Like

I think its colliding with the legs, where you arent doing anything if theyre touched. Try having it print when the legs are touched.

Update: I figured I’d share how I fixed this for anybody wondering
1: I determined that .Touched is just unreliable sometimes. It’ll be fine if you don’t need to check collisions every frame, though.
2: I got around this by using Region3 to detect collisions every frame. Since I only needed to check for one part, this didn’t have a significant hit on performance at all.
Here’s the code for that:

                local min = part.Position - (1 * part.Size)
		local max = part.Position + (1 * part.Size)
		local region = Region3.new(min, max) --This provides some room for collisions that may not happen on an exact frame. If you want it the same size as the part, change 1 to .5.
		
		--[[local p = Instance.new("Part",workspace)  --To visualize the hitbox if you need to
		p.Anchored = true
		p.CanCollide = false
		p.Transparency = 0.8
		p.Size = region.Size
		p.CFrame = region.CFrame]]
		
		for _, part in pairs(game.Workspace:FindPartsInRegion3(region,character, math.huge)) do
			runService.RenderStepped:Wait()
			if SOMETHING then
				--Code here
			end
9 Likes