Script doesn't detect model touching brick

local train1 = script.Parent.Parent.Train_1:GetDescendants()
local occupiedvalue = script.Parent.occupiedValue
function touch(traincheck)
if traincheck.Parent.Name == “Train_1” then
print(“Train1 has been stopped at station”)
script.Parent.CanCollide = true
traincheck.Parent.Anchored = true
for _,v in pairs(train1) do
if v:IsA(“BasePart”) then
v.Anchored = false
end
end
elseif occupiedvalue == 1 then
script.Parent.CanCollide = false
print(“Train has departed from station”)
for _,v in pairs(train1) do
if v:IsA(“BasePart”) then
v.Anchored = false
end
occupiedvalue = 0
end
end
end

Parent.Touched:connect(touch)
What I want in this script is for when a model with the name “Train_1” touches it, the brick will turn collide on and the mdoel will anchor. However, it doesn’t work at all. Can someone help me please?

make the function local so local function touch(traincheck) and Parent.Touched:Connect(touch())

Try this:

Brick.Touched:Connect(function(Hit)
	for i,ModelParts in pairs(Model:GetDescendants()) do
		if ModelParts:IsA("BasePart") then
			if Hit == ModelParts then
				Brick.CanCollide = true
				ModelParts.Anchored = true
			end
		end
	end
end)