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

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

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


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 

3 comments

0

why do that when you can use spawn(function() :D

1

i wonder, what do you use to get all children of something

0

GetDescendants is the most efficient loop. As it lists all possible parts, even parts that are children of the child

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