This code below works - but can lag up studio by rapidly calling it.
01 | local Hitbox = GhostObject:FindFirstChild( "Hitbox" ) |
02 | if Hitbox then |
03 | Hitbox.CanCollide = true |
04 | for i, v in pairs (Hitbox:GetTouchingParts()) do |
05 | for index, value in pairs (v.Parent:GetChildren()) do |
06 | if value.Name = = "Hitbox" then |
07 | IsColliding = true |
08 | end |
09 | end |
10 | end |
11 | Hitbox.CanCollide = false |
12 | 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:
1 | if v.Name = = "Hitbox" then -- since v.Name is apparently the type |
2 | IsColliding = true |
3 | 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.
1 | for i, v in pairs (Hitbox:GetTouchingParts()) do |
2 | if v.Name = = "Hitbox" then |
3 | IsColliding = true |
4 | end |
5 | 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?