Why i get the error attempt to index nil with 'HumanoidRootPart'

error:
ServerScriptService.Main.Tower:67: ServerScriptService.Main.Tower:18: attempt to index nil with ‘HumanoidRootPart’
script:

local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = 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 = {}



local function FindNearestTarget(newTower)

	local maxDistance = 50
	local nearestTarget = nil


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

	return nearestTarget

end

function tower.Attack(newTower)
	local target = FindNearestTarget()
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		
		
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Positon, target.HumanoidRootPart.Positon)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		
		animateTowerEvent:FireAllClients(newTower, "Attack")
		
		target.Humanoid:TakeDamage(25)
	end
	task.wait(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 bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
		bodyGyro.Parent = newTower.HumanoidRootPart
		
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:isA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
			end
		end
		
		coroutine.wrap(tower.Attack)(newTower)
		
	else
		warn("Requested tower does not exist:", name)
	end
end

spawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower

You should try debugging your code more. The error is literal about what it’s saying; you’re trying to find HumanoidRootPart in something that’s nil.

FindNearestTarget is expecting an instance with a child named HumanoidRootPart in it. There’s a case where you call it with no arguments so newTower doesn’t point to anything. Since you have no checks in your code for the existence of an object it’s assuming newTower is non-nil and then trying to find a HumanoidRootPart in it but since newTower is nil this evaluation is illegal.

Fix the way you call the function. Pass the model that has the HumanoidRootPart in it when you call FindNearestTarget. First line in the Attack function.

3 Likes

Thank you i Will try fix this :grinning: