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?
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?