Attempt to index nil with 'Name'

Hello, i have created a gun with a building system in my game.
When the gun shoots a building 4 times, it drains 0.50 hp from the building every shot. When its been shot 4 times it breaks, but on the fourth shot it fires an error. I believe this is because its trying to index it after its been destroyed, but im not quite sure.
image

I’m not really sure what to do, so please tell me if you have a soloution.
ps: (its my first time posting so sorry if theres any confusion)

if raycastresult then
			
			body.Parent = bullet
			bullet.Parent = game.Workspace.hi
			bullet.Touched:Connect(function(oP)
				if oP.Parent == game.Workspace then
					bullet:Destroy()
				end
			end)
			if raycastresult.Instance.Name == "building" then
				raycastresult.Instance.hp.Value -= 0.50
				raycastresult.Instance.Transparency += 0.25
				if raycastresult.Instance.hp.Value <= 0 then
					raycastresult.Instance:Destroy()
				end
			end
			if raycastresult.Instance.Parent.Name ~= owner then
				local humanoid = raycastresult.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
				if humanoid then
					local debris = game.ReplicatedStorage.debris:Clone()
					
					if raycastresult.Instance.Name == "Head" then
						dmg = 40
						debris.Color = Color3.new(1, 0, 0)
					end
					debris.Parent = humanoid.Parent.Torso
					debris.Position = humanoid.Parent.Torso.Position
					local goal = {}
					goal.Size = Vector3.new(6,6,6)
					local ti = TweenInfo.new(0.5)
					local tween = game.TweenService:Create(debris,ti,goal)

					humanoid.Health -= dmg
					dmg = 20
				
					tween:Play()
					local secgoal = {}
					secgoal.Size = Vector3.new(0.1,0.1,0.1)
					wait(0.1)
					db = false
					wait(0.5)
					local tween2 = game.TweenService:Create(debris,ti,secgoal)
					tween2:Play()
					wait(0.5)
					debris:Destroy()
				end
			end

Thanks!

where is the error

or

or

and what is

if raycastresult.Instance.Parent.Name ~= owner then
There.

FIXED
Moved if raycastresult.Instance.Name == “building” then
under if raycastresult.Instance.Parent.Name ~= owner then

You didn’t even provide the correct script, your error pertains to line 45. Which in the script you provided does not reference “Name”.

i cut the script to fit the situation since the error was caused by it destroying before the index, i did provide the correct script just not the whole one

No reference to the property “Name” at line 45 in the script you provided.

here >if raycastresult.Instance.Parent.Name ~= owner then

That isn’t line 45, unless you didn’t provide the entire script or it’s a module related error.

As said previously, i cut the script to fit the situation since the whole script wasnt related to the error.