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

Function not returning the thing I want it to?

Asked by 5 years ago
Edited 5 years ago

So I'm trying to make a NPC that follows you by using raycasting and Humanoid:MoveTo(), I think it's nearly done, but the problem is the function to find the target doesn't even return the thing that it's clearly supposed to. In the same indent as the return, it is supposed to print something, it does print it, but when I do print(FindTarget()), it outputs nil. This is my code, any help?

``` function FindTarget() local DONE = nil for i,v in pairs(workspace:GetChildren()) do if v:FindFirstChildOfClass("Humanoid") then local root = v:FindFirstChild("HumanoidRootPart") if root then local FastCastModule = require(script:WaitForChild("FastCast")) local FastCast = FastCastModule.new() FastCast:FireWithBlacklist(char:WaitForChild("HumanoidRootPart",1).Position,(root.Position - char.HumanoidRootPart.Position).Unit * 500,100,{script.Parent}) FastCast.RayHit:connect(function(hit,pos,,,bullet) if hit and hit.Parent then if hit.Parent == root.Parent then print(root.Position) DONE = root.Position end end end) end end if DONE ~= nil then return DONE end end end

while wait(.1) do print(FindTarget()) end --[[ Output: [Vector3] nil ]] ```

Answer this question