.Touched will not work for my hit detection script

I want a reliable way to detect a part touching another part. (That doesn’t stop firing if not moved like . Touched)
I tried using get touching parts but it broke and doesn’t do anything
.Touched has always infuriated me for just being unreliable and weird

game.Players.PlayerAdded:Connect(function(plr)

	plr.CharacterAdded:Connect(function()
		while wait() do
			
			if  plr.Character:FindFirstChild("AttackHitBox") then
				plr.Character:FindFirstChild("AttackHitBox").Touched:Connect(function(part)
					
					if part:FindFirstChild("Destroyable") and plr:FindFirstChild("Punching") then
						local Material = part.Material
						local Color = part.Color
						local Position = Vector3.new(part.Position.X + math.random(1,6),part.Position.Y + math.random(1,6),part.Position.Z + math.random(1,6))
						part:Destroy()
						for i = 1,4,1 do
							local DebrisPart = Instance.new("Part")
							DebrisPart.Size = Vector3.new(math.random(1,2),math.random(1,2),math.random(1,2))
							DebrisPart.Material = Material
							DebrisPart.Color = Color
							DebrisPart.Position = Position
							DebrisPart.Parent = game.Workspace
							fling(DebrisPart,plr.Character)
							local DespawnClone = script.DespawnScript:Clone()
							DespawnClone.Parent = DebrisPart
							DespawnClone.Disabled = false
						end
						for index = 1,4,1 do
							local CoinPart = Instance.new("Part")
							CoinPart.Size = Vector3.new(math.random(1,2),math.random(1,2),math.random(1,2))
							CoinPart.Material = Enum.Material.Metal
							CoinPart.Color = Color3.new(1, 1, 0)
							CoinPart.Position = Position
							CoinPart.Parent = game.Workspace
							fling(CoinPart,plr.Character)
							local DespawnClone = script.DespawnScript:Clone()
							DespawnClone.Parent = CoinPart
							DespawnClone.Disabled = false
						end
					end
					
				end)
			
			end

		end
			
		
	
			

		
		
	end)

end)

If you need more info ask!

I did something like this a few days ago.

local ts = game:FindService("TweenService") or game:GetService("TweenService")
local d1 = workspace.Door1
local d2 = workspace.Door2
local active = false
local istouching = false
local down = false
local cantween1 = true
local cantween2 = false

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local invisiblePart = workspace.Button
		local Ignore = char
		spawn(function()
			while wait(.1) do
				local Ray = Ray.new(char.HumanoidRootPart.CFrame.p,Vector3.new(0,-10,0))
				local part = workspace:FindPartOnRay(Ray,Ignore)
				
				if active == false then istouching = false end
				
				if part then
					if part == invisiblePart then
						active = true
					else
						active = false
					end
				end
			end
		end)
	end)
end)

spawn(function()
	game.Workspace.Button.Touched:Connect(function(part)
		if part.Parent:FindFirstChild("Humanoid") then
			istouching = true
			spawn(function()
				while true do
					wait(.1)
					if istouching == true then
						if down == false then
							down = true
							if game.Workspace.Button.Position ~= Vector3.new(-34, 0.825, -10) or cantween1 == false then return end
							local tweenInfo = TweenInfo.new(.125)
							local tween1 = ts:Create(game.Workspace.Button, tweenInfo, {CFrame = (game.Workspace.Button.CFrame * CFrame.new(0,-1,0))})
							tween1:Play()
							spawn(function()
								tween1.Completed:Connect(function()
									cantween1 = false
									cantween2 = true
								end)
							end)
						elseif down == true then
						end
					elseif istouching == false then
						down = false
						if game.Workspace.Button.Position == Vector3.new(-34, 0.825, -10) or cantween2 == false then return end
						local tweenInfo = TweenInfo.new(.125)
						local tween1 = ts:Create(game.Workspace.Button, tweenInfo, {Position = Vector3.new(-34, 0.825, -10)})
						tween1:Play()
						spawn(function()
							tween1.Completed:Connect(function()
								cantween1 = true
								cantween2 = false
							end)
						end)
						break
					end
				end
			end)
		end
	end)
end)

Hopefully this helps you :slight_smile:

Don’t loop the .touch event. the game will lag because there is lot of connection setup each millisecond

1 Like