How do I make a onTouch script work on the parts from a model?

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

local function onTouch(hit)

game.Workspace.SchoolInside.Part.Texture.Transparency = 0.5

game.Workspace.SchoolInside.Part2.Transparency = 0

game.Workspace.SchoolInside.Part3.Texture.Transparency = 0.5

game.Workspace.SchoolInside.Part4.Transparency = 0

game.Workspace.SchoolInside.Part5.Transparency = 0

game.Workspace.SchoolInside.Part6.Transparency = 0

game.Workspace.SchoolInside.Part7.Transparency = 0

game.Workspace.SchoolInside.Part8.Transparency = 0

game.Workspace.SchoolInside.Part9.Transparency = 0

game.Workspace.SchoolInside.Part10.Transparency = 0

game.Workspace.SchoolInside.Part11.Transparency = 0

game.Workspace.SchoolInside.Part12.Transparency = 0

game.Workspace.SchoolInside.Part13.Transparency = 0

game.Workspace.SchoolInside.Part14.Transparency = 0

game.Workspace.SchoolInside.Part15.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow.Part.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow.Part2.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow.Part3.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow2.Part.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow2.Part2.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow2.Part3.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow3.Part.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow3.Part2.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow3.Part3.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow4.Part.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow4.Part2.Transparency = 0

game.Workspace.SchoolInside.Door.Arrow4.Part3.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part2.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part3.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part4.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part5.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part6.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow2.Part7.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part2.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part3.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part4.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part5.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part6.Transparency = 0

game.Workspace.SchoolInside.Door.Wl_widow.Part7.Transparency = 0

game.Workspace.SchoolInside.Door.Part.Transparency = 0

end

script.Parent.Touched:Connect(onTouch)

If let’s say your model is called: ModelExample you can do this:

for k,v in pairs(ModelExample:GetChildren()) do
    if v:IsA("BasePart") then
        v.Touched:Connect(onTouch)
    end
end

Edit: for k,v in pairs(ModelExample:GetChildren()) do sorry!

can u put the code in a code block? It makes it hard to read ur code when its like that.

1 Like

And where do I need to put this? In the middle of the scripts, or at the end

Loops are very useful.

In this case you should do

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.

At the end, instead of your last line. Make sure you saved your model reference inside a ModelExample variable before.

I need to change ModelExample to the my model name?

Change this line to:

for k,v in pairs(game.Workspace.SchoolInside:GetChildren()) do
    if v:IsA("BasePart") then
        v.Touched:Connect(onTouch)
    end
end

For the school you could use something like this:

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
1 Like

Ok and in the script I need to put like this:
game.Workspace.Model.Transparency = 0

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 :stuck_out_tongue: !

Do I need to put local function onTouch (hit) at the first line?

If you only want your model to get invisible/opaque when you touch it, all you need to do is add this:

at the end, instead of this in the original script:

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

Is this good?

You don’t need the: local function onTouch(hit)

Look, try to place only this in your script:

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? :stuck_out_tongue:

I tried but it doesn’t work, do I need to ungroup any other models placed in the SchoolInside model?

Oh I didn’t know there were more models inside :smiley: .
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 :slight_smile: . 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.

Also, are you getting any errors maybe?