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

Is it possible to detect if a part is hit while using a loop?

Asked by 5 years ago
local part = Instance.new("Part")
part.CanCollide = true
part.Anchored = true
part.BrickColor = BrickColor.new("Really red")
part.Material = "Neon"
part.Parent = game.Workspace
part.Size = Vector3.new(10,10,10)
part.CFrame = game.Workspace.Spot.CFrame

for i = 1,10 do
wait(1)
part.Size = part.Size + Vector3.new(1,1,1)
end
part.Touched:connect(function(Part)
-- rest of code that is useless

Basically lets say the loop is at 5, if it is touched will it still detect it and and do the part.Touched function?

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

GetChildren and GetDescendants


Yes that would be possible, granted that you use a GetChildren or GetDescendants table, as the value portion of both those arrays are objects, and assuming those objects are base parts (parts, truss parts, mesh parts, etc.)

Which means you can do the following:

local things = workspace:GetChildren()

for _,part in pairs(things) do
    if part:IsA("BasePart") then
        part.Touched:Connect(function()
            ...
        end)
    end
end

Generic for loops


What I used here, in case you didn't know , is a generic for loop, which instead of being formatted like :

for iter = start,finish, increment do
    ...
    --print(iter)
end

and is instead is formatted like

for key, value in pairs(Table) do--next, and ipairs() also work
    ...
    --print(key,value)
end 
0
why do that when you can use spawn(function() :D greatneil80 2647 — 5y
1
i wonder, what do you use to get all children of something theking48989987 2147 — 5y
0
GetDescendants is the most efficient loop. As it lists all possible parts, even parts that are children of the child Ziffixture 6913 — 5y
Ad

Answer this question