Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to index nil with 'Parent'?

Asked by 2 years ago

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)
0
what line is the error? OfficerBrah 494 — 2y
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 OfficerBrah 494 — 2y
0
OK OK OK So I am Making A Sword But At Like 5 The local humanOther = partOther.Parent:FindFirstChild("Humanoid") kidsteve923 139 — 2y
0
Is Causing The Problem So Please Help Me I Am Dum kidsteve923 139 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

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

Ad
Log in to vote
0
Answered by
SirGamezy 186
2 years ago
Edited 2 years ago

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'))

0
dosent work kidsteve923 139 — 2y

Answer this question