.touched not registering on changing variable

I have a variable that changes every time a child is added to the scripts parent. The variable works just fine, the problem is that its not registering collisions with the part.

the code

local part

script.parent.childadded:connect(function(child)
if part:IsA("Part") then
part = child
end
end)

part.Touched:Connect(function(touch)
print(touch)
end)

The script you’re using is obviously old. You need to fix the grammar by adding upper case letters in correct places.

And it won’t work anyways if you did fix those, you are declaring part as nil when you try to check if its a part, it’ll return false because you haven’t set it as one.

local part = nil -- Declare part value and set nil

script.Parent.ChildAdded:Connect(function(child) -- Detect when ChildAdded
      if child:IsA("Part") then -- Check if the new child is a part
            part = child -- Set the part variable to the child
      end
end)

part.Touched:Connect(function(touch) -- Detect what touched the part
      print(touch)
end)

So make sure you use correct names of Objects API for example childadded should be ChildAdded.
This codes connects new touched event added to script.Parent

-- Make sure to add referemce to your part here
local part = workspace.Part

-- hit is the part that is touching part
local function touching(hit)
	print(hit)
end

script.Parent.ChildAdded:Connect(function(child)
    -- child is the object added to script.Parent
	if child:IsA("Part") then
		part = child
        -- Making new Connection for the child part added to script.Parent
		part.Touched:Connect(touching)
	end
end)

-- Making first Touch Connection for the part
part.Touched:Connect(touching)