I want to make a onTouch event, but I want it to trigger on a model (so the childs from that model will be triggered) without having to write down every single part in that script and having to give it different names, is it possible? This is my current script
for i,v in ipairs(Model:GetChildren()) -- i is the position in the list. v is the part.
if v:IsA("BasePart") then
v.Transparency = 0 -- Make it visible
end
end)
It’s better to learn from example than to copy scripts so here’s an API reference on loops.
local School = workspace.SchoolInside
for i, obj in pairs(School:GetChildren()) do
if obj:IsA("BasePart") then
obj.Transparency = 0
elseif obj:IsA("Texture") then
obj.Transparency = .5
end
end
for k,v in pairs(game.Workspace.SchoolInside:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function()
for k2,v2 in pairs(game.Workspace.SchoolInside:GetChildren()) do
if v2:IsA("BasePart") then v2.Transparency = 0 end
end
end)
end
end
But are you sure you want Transparency 0 ? If you want the model to get invisible set it to 1. Let me know if it works !
By the way, if you want this to happen only when a player touched the model, change to:
for k,v in pairs(game.Workspace.SchoolInside:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if not hit.Parent:FindFirstChild("Humanoid") then return end
for k2,v2 in pairs(game.Workspace.SchoolInside:GetChildren()) do
if v2:IsA("BasePart") then v2.Transparency = 0 end
end
end)
end
end
Sorry, I just realized that maybe you want the touch to be from a Player only.
local function onTouch(hit)
for k,v in pairs(game.Workspace.SchoolInside:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if not hit.Parent:FindFirstChild("Humanoid") then return end
for k2,v2 in pairs(game.Workspace.SchoolInside:GetChildren()) do
if v2:IsA("BasePart") then v2.Transparency = 0 end
end
end)
end
end
If you set the Transparency to 1, everything will be invisible when you touch the model, or if you set it to 0, everything will be visible when you touch the model. Is this more clear maybe?
Oh I didn’t know there were more models inside .
Change the GetChildren to GetDescendants like so:
for k,v in pairs(game.Workspace.SchoolInside:GetDescendants()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if not hit.Parent:FindFirstChild("Humanoid") then return end
for k2,v2 in pairs(game.Workspace.SchoolInside:GetDescendants()) do
if v2:IsA("BasePart") then v2.Transparency = 0 end
end
end)
end
end
Nope, still didn’t work. Is it because the script is placed into a part inside Workspace? Do I need to put that part with the script into the SchoolInside model? And I have both models and parts in the SchoolInside model
No, wherever you put the script, it should be fine . What exactly are you trying to achieve? Do you want to make the SchoolInside invisible? If so, change the Transparency to be set to 1.