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

Workspace.Dummy.FollowAndAttack:7: attempt to index nil with 'Humanoid'?

Asked by 1 year ago
Edited 1 year ago

So I have a script that checks if the humanoid exists and then attacks it, but I get the error: Workspace.Dummy.FollowAndAttack:7: attempt to index nil with 'Humanoid' on line 7 and I don't know how to fix it. Here's the line that the error is on:

if v and v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v.Humanoid and v.Humanoid.DisplayName ~= script.Parent.Humanoid.DisplayName and v.Humanoid.Health > 0 and script.Parent.Humanoid.Health ~= 0 then

And here's the full code:

01local d = false
02local maxDistance = 1000
03local PathfindingService = game:GetService("PathfindingService")
04while wait() do
05    local nearestCharacter, nearestDistance
06    for i, v in pairs(workspace:GetChildren()) do
07        if v and v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v.Humanoid and v.Humanoid.DisplayName ~= script.Parent.Humanoid.DisplayName and v.Humanoid.Health > 0 and script.Parent.Humanoid.Health ~= 0 then
08            character = v
09            local distance = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude
10            if not character or distance > maxDistance or (nearestDistance and distance >= nearestDistance)
11            then
12                continue
13            end
14            nearestDistance = distance
15            nearestCharacter = v
View all 84 lines...
0
why are you using v:FindFirstChild("Humanoid") and then v.Humanoid? Klui36 45 — 1y
0
Hey! I see your problem. in the for loop at line 6 it says "workspace:GetChidren()". What are you trying to do? maybe use :GetDescendants(). Klui36 45 — 1y
0
I'm trying to see if the children of the workspace have a humanoid. ghostgamer90391 9 — 1y
0
yes i know that lol Klui36 45 — 1y
View all comments (3 more)
0
I think its something about the script.Parent.Humanoid.Health Klui36 45 — 1y
0
Removed that, and it still gives me the same error ghostgamer90391 9 — 1y
0
replace line 7 with `if v and v:FindFirstChildOfClass("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v:FindFirstChildOfClass("Humanoid").DisplayName ~= script.Parent:FindFirstChildOfClass("Humanoid").DisplayName and v:FindFirstChildOfClass("Humanoid").Health > 0 and script.Parent:FindFirstChildOfClass("Humanoid").Health > 0 then` T3_MasterGamer 2189 — 1y

Answer this question