Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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
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 Vulkarin 581 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

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?

0
No error. Thanks for the answer. shadow7692 69 — 6y
Ad

Answer this question