I know how to connect events to a part(s) and the parts in a model. But i don’t want to type down each part in the model. Let’s say i touch a brick(TouchedEventPart) and it opens a door(wich is a model that has 52 parts in it). I want all those children to be CanCollide = false in only 1 or 2 lines of code instead of +50 lines where i type each child seperately CanCollidde = false. I know i would probably have to do something with GetAllChildren ,but i don’t really know how to use this.
I hope this made sense lol.
1 Like
You can use loops, for example:
local model = game.Workspace.Model:GetChildren() -- Put your model here..
for i,v in pairs(model) do -- Get every children in the model.
v.Touched:Connect(function(hit)
print(hit.Parent.Name .. " Touched " .. v.Name) -- prints the message.
end
end
5 Likes
In this instance you can use` Model:GetDescendants()
example:
--this example has the script parented to the brick that you touch to open the door
Door = game.Workspace.Door -- assuming Door is parented under Workspace
sp = script.Parent
sp.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then -- makes sure that what touched was a player
for _, child in pairs(Door:GetDescendants()) do --gets all children
if child:IsA("BasePart") then -- makes all baseparts in the model execute the code
child.CanCollide = false
end
end
end
end)
4 Likes