It's supposed to make the NPC check if food is nearby, however when I move the food out of the workspace or delete it and call the function again it still says there is food nearby. Can't figure out why, I would appreciate some help with this issue!
function FindResource(Type) local Head = script.Parent.Head for i, v in pairs(game.Workspace.Resources:GetChildren()) do if v then if v.Name == Type then if (v.Position - Head.Position).magnitude <= 50 then print("Found Food") end end end end end
Works fine for me, I did modify your code somewhat just to make it easier to test.
function FindResource(Type) local Head = game.Players.LocalPlayer.Character.Head for i, v in pairs(game.Workspace.Resources:GetChildren()) do if v then if v.Name == Type then if (v.Position - Head.Position).magnitude <= 50 then print("Found Food"..(v.Position - Head.Position).magnitude) end end end end end function key(inputObject,gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Z then FindResource("Part") end end game:GetService("UserInputService").InputBegan:Connect(key)