How to detect when gettouchingparts() is not touching parts

Basically GetTouchingParts tells me how many parts its touching but it doesnt turn off when its not touching anything

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()
local storage = game:GetService("ReplicatedStorage")
local org = storage:WaitForChild("Thingthatfollowsmouse")
local debounce = false
local rpressed = script.Parent.RPressed



script.Parent.MouseButton1Down:Connect(function()
	if debounce == false then
		debounce = true
		local copy = org:Clone()
		copy.Parent = workspace.Taegetfilt
		mouse.TargetFilter = workspace.Taegetfilt
		while true and wait() do
			local mousepos = mouse.Hit.p
			copy.PrimaryPart.Position = (mousepos + Vector3.new(0, 0.5, 0))
			local touchingparts = copy.PrimaryPart:GetTouchingParts()
			
			script.Parent.RPressed.Changed:Connect(function()
				if rpressed.Value == 1 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 90, 0)
				elseif rpressed.Value == 2 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 180, 0)
				elseif rpressed.Value == 3 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 270, 0)
				elseif rpressed.Value == 4 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 360, 0)
				end
			end)
			
			for i=1,#touchingparts do
				print(#touchingparts)
				if #touchingparts < 0 then
					copy.PrimaryPart.Color = Color3.new(0, 170, 0)	
				end
				if #touchingparts > 0  then
					if touchingparts[i].Parent.Parent == workspace.Taegetfilt then
						copy.PrimaryPart.Color = Color3.new(0.580392, 0, 0)
					end				
				end				
			end
		end
	end
end)```

I am getting no errors in output, please help

Try doing
if #touchingparts <= 0 then

< means less than meaning it has to be less than 0 while <= is less or equals to.

But if I were you I’d just do if #touchingparts == 0 then

1 Like

GetTouchingParts() returns a table of parts of which are currently touching the part the function was called on. In Lua we can use the length operator which is denoted by the hash symbol (#) in order to ascertain the length of a table (the total number of entries within that table) as such if the length is 0 that would indicate that the returned table value is empty meaning that currently the part of which “GetTouchingParts()” was called on is currently touching 0 parts.

1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()
local storage = game:GetService("ReplicatedStorage")
local org = storage:WaitForChild("Thingthatfollowsmouse")
local debounce = false
local rpressed = script.Parent.RPressed



script.Parent.MouseButton1Down:Connect(function()
	if debounce == false then
		debounce = true
		local copy = org:Clone()
		copy.Parent = workspace.Taegetfilt
		mouse.TargetFilter = workspace.Taegetfilt
		while true and wait() do
			local mousepos = mouse.Hit.p
			copy.PrimaryPart.Position = (mousepos + Vector3.new(0, 0.5, 0))
			local touchingparts = copy.PrimaryPart:GetTouchingParts()

			script.Parent.RPressed.Changed:Connect(function()
				if rpressed.Value == 1 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 90, 0)
				elseif rpressed.Value == 2 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 180, 0)
				elseif rpressed.Value == 3 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 270, 0)
				elseif rpressed.Value == 4 then
					copy.PrimaryPart.Orientation = Vector3.new(0, 360, 0)
				end
			end)
			if #touchingparts <= 0 then
				return
			end
			for i=1,#touchingparts do
				print(#touchingparts)
				if #touchingparts < 0 then
					copy.PrimaryPart.Color = Color3.new(0, 170, 0)	
				end
				if #touchingparts > 0  then
					if touchingparts[i].Parent.Parent == workspace.Taegetfilt then
						copy.PrimaryPart.Color = Color3.new(0.580392, 0, 0)
					end				
				end				
			end
		end
	end
end)

Here I’ve added a conditional which checks the length of the table value returned from the “GetTouchingParts()” call, if the length is less than or equal to 0 then the function returns and the remainder of the code within the function is skipped over (not executed), otherwise if the length is greater than 0 then the remaining code belonging to the function is executed.

1 Like