Attempt to index nil with 'Parent'

I’m trying to make a gun system for my group using raycasting.

Each time I shoot somewhere off Into the distance an error appears. All the script below the line that Is creating an error just stops because of It.
image

I have tried to look everywhere but I can’t find a solution.

Here Is my script. It Is pretty messy but I hope you can find the Issue

script.Parent.RayCastEvent.OnServerEvent:Connect(function(Player, FromP, ToP)
	local RayCast = Ray.new(FromP, (ToP-FromP).unit * 100)
	local Part, Position, Normal = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true)
	script.Parent.Handle.ShootSound:Play()
	script.Parent.PlayAnimShoot:Fire()
	local Hole = Instance.new("Part", Part.Parent)
	local HoleImage = Instance.new("Decal", Hole)
	
	if Part then
		local Humanoid = Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			if game.Players:FindFirstChild(Humanoid.Parent.Name) then
				local enemy = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
				if Player.TeamColor ~= enemy.TeamColor then
					Humanoid:TakeDamage(25)
					local HitSound = script.HitSound:Clone()
					HitSound.Parent = Hole
					HitSound:Play()
					if Part.Name == "Head" then
						Humanoid:TakeDamage(25)
					end
				end
			else
				Humanoid:TakeDamage(25)
				local HitSound = script.HitSound:Clone()
				HitSound.Parent = Hole
				HitSound:Play()
				if Part.Name == "Head" then
					Humanoid:TakeDamage(25)
				end
			end
		else
			local NonHitSound = script.NonHitSound:Clone()
			NonHitSound.Parent = Hole
			NonHitSound:Play()
		end
	end
	local Bullet = Instance.new("Part", game.Workspace)
	Bullet.CanCollide = false
	Bullet.Anchored = true
	Bullet.BrickColor = BrickColor.new("Gold")
	Bullet.Material = Enum.Material.Neon
	
	script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
	
	local Dist = (ToP-FromP).magnitude
	local TweenService = game:GetService("TweenService")
	local TweenInformation = TweenInfo.new(0.05)
	Bullet.CFrame = CFrame.new(FromP, Position)
	local BulletMove = TweenService:Create(Bullet, TweenInformation, {Position = ToP})
	BulletMove:Play()
	Bullet.Size = Vector3.new(0.15, 0.15, 0.35)
	Bullet.Transparency = 0.2
	Bullet.Name = "Bullet"
	local mesh = Instance.new("SpecialMesh", Bullet)
	mesh.MeshType = Enum.MeshType.Sphere
	
	script.Parent.Hole.FlashGui.Enabled = true
	script.Parent.Hole.FlashLight.Enabled = true
	BulletMove.Completed:Connect(function()
		game.Debris:AddItem(Bullet, 0)
	end)
	
	Hole.Size = Vector3.new(0.8, 0.8, 0.05)
	Hole.Name = "BulletHole"
	Hole.Transparency = 0
	HoleImage.Texture = "http://www.roblox.com/asset/?id=64291977"
	Hole.Anchored = false
	Hole.CanCollide = false
	HoleImage.Face = "Front"
	Hole.Transparency = 1
	Hole.Position = ToP
	Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+Normal)
	game.Debris:AddItem(Hole, 120)
	if Part.Parent ~= "" then
		if Part.Parent:FindFirstChild("Humanoid") or Part.Parent.Parent:FindFirstChild("Humanoid") then
			HoleImage.Texture = "https://www.roblox.com/library/5190093324/BulletHit"
		end
	end
	if Part.Parent ~= "Workspace" or Part ~= "Workspace" then
		local Weld = Instance.new("Weld")
		Weld.Part0,Weld.Part1 = Part, Hole
		Weld.C0 = Part.CFrame:Inverse()
		Weld.C1 = Hole.CFrame:Inverse()
		Weld.Parent = Hole
	end
	
	wait(0.1)
	script.Parent.Hole.FlashGui.Enabled = false
	script.Parent.Hole.FlashLight.Enabled = false
end)

Line 19 Is

local Hole = Instance.new("Part", Part.Parent)

The problem is that script.HitSound might not exist. Try check if the sound object actually exists.

Which line errors? I’ve counted to line 19, which seems to be this.

if Part.Name == "Head" then

I don’t see any “Parent” here, so I’m assuming this is not the error line.

Line 19 Is

local Hole = Instance.new("Part", Part.Parent)

Sorry If I’m not specific.

Il edit the topic.

that basically means that the value for Part doesn’t exist ( is nil )
so in your case, you created Hole and parent it to Part before checking if Part exists

1 Like

This is probably because no part could be found, therefore Part is nil. You should check if the part actually exists.

if Part then
    --yippee, part was found
else
    --oops, no part found
end
1 Like

Thank you for helping me! I appreciate It allot!

Thank you aswell! Next time Il be more specific If I need some help.