HumanoidRootPart is not a valid member of

So I was following a tutorial but I am stuck because I got an error but I dont know how to fix it maybe you know whats wrong
my error:
7123c6595953eda34e7a3c9e29e924df
my script:

local PS = game:GetService("PhysicsService")
local SS = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("Events")
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
local AnimateTowerEvent = events:WaitForChild("AnimateTower")

local tower = {}

function FindNearestTarget(newTower, range)
	local nearestTarget = nil

	for i, target in ipairs(workspace.Enemys:GetChildren()) do
		local distance  = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < range then
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget
end

function tower.Attack(newTower)
	local config = newTower.Config
	local target = FindNearestTarget(newTower, config.Range.Value)
	
	if target and target:WaitForChild("Humanoid") and target.Humanoid.Health > 0 then
		
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		newTower:WaitForChild("HumanoidRootPart").CFrame = targetCFrame
		
		AnimateTowerEvent:FireAllClients(newTower, "Attack")
		
		target:WaitForChild("Humanoid"):TakeDamage(config.Damage.Value)
		
		task.wait(config.Cooldown.Value)
	end
	
	task.wait(0.1)
	
	tower.Attack(newTower)
end

function tower.Spawn(player, name, cframe)
	local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
	
	if towerExists then
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		
		local CFrame = Instance.new("CFrameValue")
		CFrame = Vector3.new(math.huge, math.huge, math.huge)
		CFrame = newTower.HumanoidRootPart.CFrame
		
			for i, object in ipairs(newTower:GetDescendants()) do
				if object:IsA("BasePart") then
					PS:SetPartCollisionGroup(object, "Tower")
				end
		end	
		
		coroutine.wrap(tower.Attack)(newTower)
	else
		warn("tower doesnt exitst;", name)
	end
end

SpawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower

idk whats wrong but on line 18 maxDistance is also marked red for some reason.

I’m guessing that is the root error (line 31).

A solution could be validating the HRP and the target instead of directly indexing them like the following code:

function FindNearestTarget(newTower, range)
	if newTower == nil or not newTower:FindFirstChildWhichIsA("BasePart") then return end		-- Validating if the "newTower" exists and if it has an HRP.
	local nearestTarget = nil

	for i, target in ipairs(workspace.Enemys:GetChildren()) do
		if not target:FindFirstChildWhichIsA("BasePart") then return end		-- Checking if the root part exists and cancels the execution if not.
		
		local distance  = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
		if distance < range then
			nearestTarget = target
			maxDistance = distance
		end
	end

	return nearestTarget
end

Ty this fixed this problem but I got random properties in every asset in workspace when I try to play it? but I didnt put them there do you know why this could happen.
the weird thing is that they dont even have a name or anything
fffb925df598dbd659c2a562eaa5e9c1
a37dbaed453c15f0b74da6780f5ef3db