Attempt to index nil with 'Parent

Im trying to make a damage to my gun but whenever i miss it gives me a error
image
i have tried alot of things trying to fix this issue but nothing is working

heres an piece of the code


function DamageCast()
	local humanoid = mouse.Target.Parent:FindFirstChild("Humanoid")
	
	if not humanoid then
		return nil
	end
	
	
	if humanoid then
		humanoid.Health = humanoid.Health - math.random(40,55)
		
		
	else
		return
	end
	
end

That would mean the parent of the mouse target does not exist. This is possibly because your mouse is not hitting anything at the moment to fix this here’s a modified version of your script:

function DamageCast()
        if mouse.Target then
           local humanoid = mouse.Target.Parent:FindFirstChild("Humanoid") 
        end

	
	if not humanoid then
		return nil
	end
	
	
	if humanoid then
		humanoid.Health = humanoid.Health - math.random(40,55)
		
		
	else
		return
	end
	
end
4 Likes

It did work but when i shoot at an humanoid sometimes it doesn’t even damage or it damages annother humanoid im not aiming at!

Is this a localscript? You can’t deal damage through a local script.

2 Likes

what should i add here
image

fyi dont do any of that casting on local scripts OR EVEN DAMAGE THE HUMANOID ON CLIENT. you should connect the mouse data to a remote event and let the server do the raycasting etc.

Also if you raycast using mouse target itself anyone can just damage by just looking by a wall and clicking on the player. Lots of errors on this one.

So i should use raycast on my gun?

Ofc ofc. Also drop the client stuff. Do everything that doesnt include getting input in server scripts

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