Getting Children of a Model

How would I go about calling every children of a model? Like for example. I want every part of “Bus” to be transparent, ontouch.

Here’s my attempt on the example.

BusModel = game.Workspace.Bus
BusParts = BusModel:GetChildren()

function onTouched ()
	
	BusParts.Transparency = 1
	
end

script.Parent.Touched:Connect (onTouched)

Doesn’t work at all, I need an explanation for this since I’m trying my best to become a scripter. Thanks!

You would use a for loop to loop through all the children. For example:

for _, Part in ipairs(BusModel:GetChildren()) do -- We're looping through all the children of "BusModel". Note this includes all objects (folders, other models, etc)
       if Part:IsA("BasePart") then -- make sure its a part
            Part.Transparency = 1;
       end;
end;
1 Like

Where would I put this in the script though? Inside of the ontouch or above it?

You would put it inside your onTouched() function:

function onTouched ()
	
for _, Part in ipairs(BusModel:GetChildren()) do -- We're looping through all the children of "BusModel". Note this includes all objects (folders, other models, etc)
       if Part:IsA("BasePart") then -- make sure its a part
            Part.Transparency = 1;
       end;
end;
	
end

There might be typos so just double check in the script editor.

1 Like

Thank you, this helped a lot! Now I just have to practice and learn GetChildren, this might take a while. :eyes:

No worries, keep at it and soon this stuff will be easy! Goodluck!

1 Like
local Players = game:GetService("Players")

local BusModel = workspace.Bus
local Trigger = script.Parent

local function OnTouched(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if Player then
		for _, BusPart in ipairs(BusModel:GetChildren()) do
			if BusPart:IsA("BasePart") then
				BusPart.Transparency = 1
			end
		end
	end
end

Trigger.Touched:Connect(OnTouched)

You should declare variables/define functions locally (by preceding them with the local keyword), by doing this they are integrated into the local environment which allows for them to accessed/referenced much quicker when necessary (as doing this circumvents the need to index the global environment table in order to fetch their values). You should also be checking that the “Touched” event of the trigger part fired because it was touched by a player’s character otherwise any touching part will result in the connected function executing fully.

1 Like