Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to get the Name of a part from GetTouchingParts() function?

This code below works - but can lag up studio by rapidly calling it.

			local Hitbox = GhostObject:FindFirstChild("Hitbox")
			if Hitbox then
				Hitbox.CanCollide = true
				for i, v in pairs(Hitbox:GetTouchingParts()) do				
					for index, value in pairs(v.Parent:GetChildren()) do
						if value.Name == "Hitbox" then
							IsColliding = true
						end
					end
				end	
				Hitbox.CanCollide = false
			end

I have had to use two for i, v in pairs loops because for some reason, GetTouchingParts() cannot return the name, in v.Name, For example this won't work:

				
					if v.Name == "Hitbox" then -- since v.Name is apparently the type
						IsColliding = true
					end

Is there another way to obtain the same result but without the second loop?

1 comment

1

bootsiels answer should work fine but a note for the future for you there is a method called FindFirstChild() and it does the same thing

1 answer

✓ Accepted answer

I just tested this and you should be able to it without the second loop like this.

for i, v in pairs(Hitbox:GetTouchingParts()) do
	if v.Name == "Hitbox" then
		IsColliding = true
	end
end

GetTouchingParts() returns an array of the object "BasePart". This object includes the name and other properties like you'd expect. If I make "Hitbox" a part which is touching a Baseplate then the function GetTouchingParts() will return only the Baseplate object. Which you can then get the name from. It does not return the original part which you are using the function on. Do you get any errors when you try to do this?

1 comment

0

No error. Thanks for the answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.