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

The SchoolInside is invisible by default, I want it to become visible once you touch a invisible part which doesn’t collide.
I don’t see any errors in the script, and it shows nothing about it in Output

Hmm okay, where do you keep the script then? Just curious. I ran it on my studio and it works well.

Inside a part which is placed inside Workspace

Nvm it works now, I just need to touch the model self, but I want it to become visible earlier so when touching a invisible part not connected to the model.

Oh okay I understand. Then what I recommend is, build a big invisible box (a simple cube part) around your school, name it for example CollisionBoxPart and change the script to:

local invis = true
game.Workspace.CollisionBoxPart.Touched:Connect(function(hit)
    if not invis then return end invis = false
    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)

This way, you can make the box as big as you want, depending on how far away you want players to be before the school turns visible. Tell me if this works.

Also, make sure you uncheck the CanCollide property of the box and check the Anchored !

image

Does it need to be big? The room where the part is placed is small and the part self is set, with CanCollide unchecked and Anchored true, and it needs to be inside the school model or that doesn’t matter where it is placed?

Place the part wherever you want, it’s just that when players touch it, the school will turn visible. It doesn’t need to be big, it needs to be placed where you want player to be when the school turns visible again, is this more clear ?

For example:
Initial before touching the box:


After I touch the box:

So I can make the box as big as I want, depending on where I want the player to stand before turning the school visible!

I see but for me it doesn’t work, the CollisionBox needs to be the part that makes the school visible onTouch right?

Yes. Are you sure your box is anchored, and it can be touched?

Also, make sure that the CollisionBox is under game.Workspace ! If you get any errors please let me know :slight_smile:

Yes, Anchored is enabled and CanTouch is enabled too. Do I need to change it from game.Workspace.SchoolVisible.Touched:Connect(function(hit) to script.Parent.Touched:Connect(function(hit)?
The CollisionBox is placed under game.Workspace

Oh maybe I did a mistake? Try this:

local invis = true
game.Workspace.CollisionBoxPart.Touched:Connect(function(hit)
    if not invis then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    invis = false
    for k2,v2 in pairs(game.Workspace.SchoolInside:GetDescendants()) do
        if v2:IsA("BasePart") then v2.Transparency = 0 end
    end
end)

A big adventure, but we managed to solve it :smiley: !

1 Like

Yes, this works, now to make it invisible I need to change Transparency = 0 to Transparency = 1 in the other part?

Supposing you want to turn it again invisible after another CollisionBox is touched, let’s call it CollisionBox2, you need to:

local invis = true

--Touching CollisionBoxPart makes the school VISIBLE
game.Workspace.CollisionBoxPart.Touched:Connect(function(hit)
    if not invis then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    invis = false
    for k2,v2 in pairs(game.Workspace.SchoolInside:GetDescendants()) do
        if v2:IsA("BasePart") then v2.Transparency = 0 end
    end
end)

--Touching CollisionBoxPart2 makes the school INVISIBLE
game.Workspace.CollisionBoxPart2.Touched:Connect(function(hit)
    if invis then return end
    if not hit.Parent:FindFirstChild("Humanoid") then return end
    invis = true
    for k2,v2 in pairs(game.Workspace.SchoolInside:GetDescendants()) do
        if v2:IsA("BasePart") then v2.Transparency = 1 end
    end
end)

Make sure you the two CollisionBoxes don’t touch each other! Please tell me if this is okay, otherwise we can figure out another solution as well :stuck_out_tongue:

Yes, this is okay, now to skip certain parts to only show the textures from these parts (under these parts) with transparency 0.5 I need to put these parts in Workspace or something?

So, you have some parts that you want to always be invisible, but they have textures, and when the school turns visible, you want these textures to be 0.5 Transparent?

1 Like

Yep, correct and the textures are put as child of these parts

Okay gotcha!

Test this:

local invis = true

--Touching CollisionBoxPart makes the school VISIBLE
game.Workspace.CollisionBoxPart.Touched:Connect(function(hit)
	if not invis then return end
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	invis = false
	for k2,v2 in pairs(game.Workspace.SchoolInside:GetDescendants()) do
		if v2:IsA("BasePart") then
			--if we find a texture inside the part, only change the texture, else change the part
			if v2:FindFirstChild("Texture") then
				v2.Texture.Transparency = .5
			else
				v2.Transparency = 0
			end
		end
	end
end)

--Touching CollisionBoxPart2 makes the school INVISIBLE
game.Workspace.CollisionBoxPart2.Touched:Connect(function(hit)
	if invis then return end
	if not hit.Parent:FindFirstChild("Humanoid") then return end
	invis = true
	for k2,v2 in pairs(game.Workspace.SchoolInside:GetDescendants()) do
		if v2:IsA("BasePart") then
			--if we find a texture inside the part, only change the texture, else change the part
			if v2:FindFirstChild("Texture") then
				v2.Texture.Transparency = 1
			else
				v2.Transparency = 1
			end
			 
		end
	end
end)
1 Like