Attempt to index nil with 'Parent'

20:29:07.012 Workspace.Beretta M9 1985.Script:28: attempt to index nil with ‘Parent’ - Server - Script:28

local damage = script.Parent.Damage.Value
local ReloadTime = script.Parent.ReloadTime.Value
local FireRate = script.Parent.FireRate.Value
local MaxAmmo = script.Parent.MaxAmmo.Value
local ammo = script.Parent.Ammo.Value

local canFire = true

local fireAnim = script.Parent.Fire
local reloadAnim = script.Parent.Reload
local track

local function reload()
	canFire = false
	track = script.Parent.Parent.Humanoid:LoadAnimation(reloadAnim)
	track.Priority = Enum.AnimationPriority.Action
	track.Looped = true
	track:Play()
	script.Parent.Handle.Reload:Play()
	wait(ReloadTime)
	ammo = MaxAmmo
	canFire = true
end

local function fire(target)
	if ammo == 1 or ammo > 1 then
		canFire = false
		if target.Parent:FindFirstChild("Humanoid") then
			local Humanoid = target.Parent:FindFirstChild("Humanoid")
			Humanoid:TakeDamage(damage)
		else return end
		track = script.Parent.Parent.Humanoid:LoadAnimation(fireAnim)
		track.Priority = Enum.AnimationPriority.Action
		track.Looped = true
		track:Play()
		script.Parent.Handle.Fire:Play()
		ammo -= 1
		wait(FireRate)
		canFire = true
	elseif ammo == 0 then
		canFire = false
		script.Parent.Handle.No_Ammo:Play()
		wait(FireRate)
		canFire = true
	end
end

script.Parent.ShootEvent.OnServerEvent:Connect(function(player,targetA,args)
	if args == "fire" then
		local target = targetA
		if canFire == true then
			fire(target)
		else
			return
		end
	elseif args == "reload" then
		reload()
	end
end)

try checking if target exists first, THEN check for its parent, e.g:

if (target and target.Parent:FindFirstChild("Humanoid")) then
--code

its not shooting but now its doesn’t printing anything

you’re returning the entire statement if a target is missing which stops it from continuing - try removing else return end

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.