Touched workaround

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    -make a part that can receive a .Touched when anchored
  2. What is the issue? Include screenshots / videos if possible!
    -i have a part that updates its position but the “Touched” doesnt work like that
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    -Couldn’t find any
script.Plr.Value = script.Parent.Parent.Parent
script.Tool.Value = script.Parent.Parent
script.Parent.Parent = workspace
local hh = false
local moved = 0
repeat
	wait()
	script.Parent.Touched:Connect(function(hit)
		if hh then return end
		if not hit ==  script.Tool.Value.Handle then
			hh = true
			print("i touched the floor")
		end
	end)
	script.Parent.Position += script.Parent.CFrame.LookVector * 1
	moved = moved + 1
until hh
script.Parent.Size = script.Parent.Size * Vector3.new(1, 1, moved) --make sure its start is X, Y, 1
local moveback = moved * -.5
script.Parent.Position += script.Parent.CFrame.LookVector * moveback 
script:Destroy()

Try workspace:GetPartsInPart().

1 Like

is that a boolean or is it a list

It returns a table. Just click on the link to the documentation.

1 Like

i keep gettin “Argument 1 missing or nil”

local list = workspace:GetPartsInPart()
	if #list > 0 then
		for i, v in workspace:GetPartsInPart() do
			if hh then return end
			if v == nil then return end
			print(v.Name)
			if v ~=  script.Tool.Value.Handle then
				hh = true
				print("i touched the floor")
			else
				print("i touched something else")
			end
		end
	end
end

As per the documentation, :GetPartsInPart(part) takes a part as its first parameter. This will return a table with all the parts inside the parameter part when executed.

1 Like

Error Explanation

The main reason why your code isn’t working as expected is because the BasePart.Touched event only fires when another part intersects/impacts the anchored part’s geometry (or vice versa) after the game calculates the physics updates for that frame.

When you Anchor a part, the game engine no longer calculates its physics because it no longer has the capability to move. However, if another part touches it, the anchored part can still receive a Touched event as expected, because the other part is having its physics calculated by the game engine every physics frame and therefore it passes the message onto the anchored part.

This means that if you have one anchored part and move it by changing its CFrame or Position property, and it overlaps with another anchored part, neither of their physics are calculated and therefore neither part receives a Touched event.

Solution

I won’t say too much here as it seems like you’re moving to the right solution using GetPartsInPart as per the other’s suggestions, I just wanted to explain why your original method wasn’t working!


Another thing to keep in mind is that it’s not good to connect new event functions within a loop as you did here with the Touched event in the repeat:

repeat
	wait()
	script.Parent.Touched:Connect(function(hit)
		if hh then return end
		if not hit ==  script.Tool.Value.Handle then
			hh = true
			print("i touched the floor")
		end
	end)
	script.Parent.Position += script.Parent.CFrame.LookVector * 1
	moved = moved + 1
until hh

– as this creates a new listen callback for that Touched every single time. Meaning, if the repeat runs ten times, and that part is touched, it will trigger the touched function code ten times, which can quickly lead to unintended behaviour and poor game performance.
(Someone more nerdy than me correct me if I’m wrong on that last part :stuck_out_tongue_winking_eye:)

repeat
	local list = workspace:GetPartsInPart(script.Parent)
	for i, v in list do
		if hh then return end
		print(v.Name)
		if v ~=  script.Tool.Value.Handle then
			hh = true
			print("i touched the floor")
		else
			print("i touched something else")
		end
	end
end
until not hh

Don’t worry random developer forum user, I was warning OP against using this since it was in their original code. :x::trophy:

O-

Sorry It’s 3 AM way past my bed time and I am not in the right place. Forgive me for such outrageous words that I wrote. Have a good night/day, random roblox’s development forum user!