Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to index nil with 'Parent'?

What? Pls Fix This Idk How To

local tool = script.Parent

local function onTouch(partOther)
	
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")
	
	if not humanOther then return end
	
	if humanOther.Parent == tool then return end
	
	humanOther:TakeDamage(25)
end

tool.Activated:Connect(onTouch)

4 comments

0

what line is the error?

0

the error means you're trying to access the Parent property of something that doesn't exist but you need to give more information about the problem

0

OK OK OK So I am Making A Sword But At Like 5 The local humanOther = partOther.Parent:FindFirstChild("Humanoid")

0

Is Causing The Problem So Please Help Me I Am Dum

2 answers

This should happen when the sword touches something, since that will tell the part that touched it

tool.Touched:Connect(onTouch)

edit: also place this script in the weapon's handle

The error is in the line:

tool.Activated:Connect(onTouch)

The function onTouch has one parameter: partOther. When the tool is activated, you called the function onTouch, but you didn't give it any parameters. Therefore partOther would be nil. Fix:

local tool = script.Parent
	 
local function onTouch(partOther)
 
	local humanOther = partOther.Parent:FindFirstChild("Humanoid")

	if not humanOther then return end

	if humanOther.Parent == tool then return end
	 
	humanOther:TakeDamage(25)
end
	 
tool.Activated:Connect(onTouch('whatever partOther is'))

1 comment

0

dosent work

This discussion is preserved as it was. New answers, comments, and votes are closed.