so i make a gun script, if mouse.target.parent is a humanoid, it will take a damage but i got an error

Players.BlackBooks.Backpack.Gun.MainScript:30: attempt to index nil with 'Parent'

this is the script

script.Parent.Activated:Connect(function()
		track = tool.Parent.Humanoid:LoadAnimation(anim)
		track.Priority = Enum.AnimationPriority.Action
		track.Looped = false
		if ammo > 0 and not reload and not deb then
			deb = true
			ammo = ammo - 1
			script.Parent.FireSound:Play()
			track:Play()
			if mouse.Target.Parent:FindFirstChild("Humanoid") then -- this is line 30
				if mouse.Target.Parent.Name ~= player.Name then
					script.Parent.DealDamage:FireServer(mouse.Target.Parent, 20)
				end
			end
			wait(0.5)
			deb = false
		elseif reload == false and ammo == 0 then
			reloads()
		end
		while wait() do
			text.Text = ammo.." / "..maxAmmo
		end
	end)

and this is the full script, just so you can see it clearly

local maxAmmo = 10
local ammo = maxAmmo
local reload = false
local player = game.Players.LocalPlayer
local gui = player:WaitForChild("PlayerGui")
local text = gui:WaitForChild("Ammo"):FindFirstChild("Ammo")
local deb = false
local tool = script.Parent
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://5992010544"
local track

script.Parent.Equipped:Connect(function(mouse)
	local function reloads()
		reload = true
		script.Parent.Reload:Play()
		wait(1.5)
		ammo = maxAmmo
		reload = false
	end
	script.Parent.Activated:Connect(function()
		track = tool.Parent.Humanoid:LoadAnimation(anim)
		track.Priority = Enum.AnimationPriority.Action
		track.Looped = false
		if ammo > 0 and not reload and not deb then
			deb = true
			ammo = ammo - 1
			script.Parent.FireSound:Play()
			track:Play()
			if mouse.Target.Parent:FindFirstChild("Humanoid") then -- this line is error
				if mouse.Target.Parent.Name ~= player.Name then
					script.Parent.DealDamage:FireServer(mouse.Target.Parent, 20)
				end
			end
			wait(0.5)
			deb = false
		elseif reload == false and ammo == 0 then
			reloads()
		end
		while wait() do
			text.Text = ammo.." / "..maxAmmo
		end
	end)
	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.R and reload == false and ammo ~= maxAmmo then
			reloads()
		end
	end)
end)

i appreciate every helpers. Thanks for replying

1 comment

0

oh i've got the answer, it is because mouse.Target is nil